java with xml parsing

java with xml parsing

Hi,

I need the sample code for parsing complex data xml file with java code.
Example product,category,subcategory these type of xml files and parse using java.
Please send the code immediately its very urgent.


this is my xml file....

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
<product description="Cardigan Sweater" product_image="cardigan.jpg">
<catalog_item gender="Men's">
<item_number>QWZ5671</item_number>
<price>39.95</price>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Large">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
</catalog_item>
<catalog_item gender="Women's">
<item_number>RRX9856</item_number>
<price>42.50</price>
<size description="Small">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
</size>
<size description="Medium">
<color_swatch image="red_cardigan.jpg">Red</color_swatch>
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Large">
<color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
<size description="Extra Large">
<color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
<color_swatch image="black_cardigan.jpg">Black</color_swatch>
</size>
</catalog_item>
</product>
</catalog>

thanks,
Valarmathi P
View Answers

February 2, 2010 at 3:38 PM

Hi Friend,

Try the following code:

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class DOMParserExample {
public final static String getElementValue( Node elem ) {
Node node;
if( elem != null){
if (elem.hasChildNodes()){
for( node = elem.getFirstChild(); node != null; node = node.getNextSibling() ){
if( node.getNodeType() == Node.TEXT_NODE ){
return node.getNodeValue();
}
}
}
}
return "";
}
public static void retrieveValue(Node node,int index) {
String nodeName = node.getNodeName();
String nodeValue=getElementValue(node);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 1; i++) {
buffer.append(" ");
}
System.out.println( "NodeName: " + nodeName + ", NodeValue: " + nodeValue);
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.ELEMENT_NODE) {
retrieveValue(child,index + 2);
}
}
}
public static void main(String[] args) throws Exception {
File f=new File("c:/catalog.xml");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(f);
Node root = doc.getDocumentElement();
retrieveValue(root,0);
}
}

Hope that it will be helpful for you.
Thanks

January 25, 2012 at 3:10 PM

can you pelase provide a code for the same using SAX


June 7, 2013 at 12:59 PM

hi friend,

Please go through the link, may this will be helpful for you. Here you will found an another example using SAX

http://www.roseindia.net/xml/sax/EmployeeDetails.shtml

Thanks.









Related Tutorials/Questions & Answers:
XML parsing using Java - XML
XML parsing using Java  I'm trying to parse a big XML file in JAVA. The goal is like i will take console input from user until "Coverage" in element "MIRate"(THE XML code is below).once he enters coverage rate I need
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
Advertisements
java with xml parsing - Java Beginners
java with xml parsing  Hi, I need the sample code for parsing complex data xml file with java code. Example product,category,subcategory these type of xml files and parse using java. Please send the code immediately its very
java with xml parsing - Java Beginners
java with xml parsing  Hi, I need the sample code for parsing complex data xml file with java code. Example product,category,subcategory these type of xml files and parse using java. Please send the code immediately its very
parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following
xml parsing
xml parsing  my task is when i wll give java file to a program that program interpret that file separate each and every keywords and indentifiers... xml.. please anyone of you give some idea
Xml parsing
Xml parsing  Hi..i tried the above code but I unable to know where can i place the xml file
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
xml parsing - XML
± how can i do this?   Hi friend, Parsing The XML File using DOM parser in JSP visit to : http://www.roseindia.net/jsp/parsing-xml.shtml...xml parsing  Hi i want to parse the xml document which contains US en
XML parsing to Mysql database
XML parsing to Mysql database  Can someone please post the code for parsing an XML file into Mysql database using SAX
Question about parsing XML document with DOM in JAVA.
Question about parsing XML document with DOM in JAVA.  Hi, I need to parse a XML file witch has info about classes. So i have for each class to have... it with multiple for loops, one iside the other but it doesnt work. My XML is this below
parsing xml with jquery
parsing xml with jquery  Please explain how to parse a xml file into jquery. Thanks
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
xml document parsing
xml document parsing   Hi Guys, Xml document has been inserted into the database,how do i parse the xml document stored in database,so that xml document can be retrieved on to JSP file using jdbc/odbc connection. Please help me
XML Parsing from php web server in J2ME
XML Parsing from php web server in J2ME  Hello Team, You gave some tips for XML parsing in java, i need some additional tips for xml parsing from php web server in j2me... Thanks and Regards Alagu
problems in parsing the xml with the special characters
problems in parsing the xml with the special characters  Hi, I have a problem, in while parsing the xml with special characters apstrophe('). I am... parse this string in xml path. i am not able to fetch the adam's string in the xml
XML Parsing Using Sax Parser in J2ME for serverside
XML Parsing Using Sax Parser in J2ME for serverside  Hai team, i have doubt in Parsing using sax parser in serverside plz help me for xml parsing in j2me using sax parser on server side thanks in advance... regards Selva
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.28.0.1058 is released. Learn to use sonar-xml-parsing version 1.28.0.1058 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.28.0.1058 java library... 1.28.0.1058 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.23.0.957 is released. Learn to use sonar-xml-parsing version 1.23.0.957 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.23.0.957 java library in your... 1.23.0.957 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.24.0.965 is released. Learn to use sonar-xml-parsing version 1.24.0.965 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.24.0.965 java library in your... 1.24.0.965 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.22.0.848 is released. Learn to use sonar-xml-parsing version 1.22.0.848 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.22.0.848 java library in your... 1.22.0.848 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.27.0.1040 is released. Learn to use sonar-xml-parsing version 1.27.0.1040 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.27.0.1040 java library... 1.27.0.1040 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.10.1.446 is released. Learn to use sonar-xml-parsing version 1.10.1.446 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.10.1.446 java library in your... 1.10.1.446 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.21.0.829 is released. Learn to use sonar-xml-parsing version 1.21.0.829 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.21.0.829 java library in your... 1.21.0.829 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.20.0.801 is released. Learn to use sonar-xml-parsing version 1.20.0.801 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.20.0.801 java library in your... 1.20.0.801 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.19.0.786 is released. Learn to use sonar-xml-parsing version 1.19.0.786 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.19.0.786 java library in your... 1.19.0.786 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.18.0.756 is released. Learn to use sonar-xml-parsing version 1.18.0.756 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.18.0.756 java library in your... 1.18.0.756 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.17.0.740 is released. Learn to use sonar-xml-parsing version 1.17.0.740 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.17.0.740 java library in your... 1.17.0.740 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.16.0.719 is released. Learn to use sonar-xml-parsing version 1.16.0.719 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.16.0.719 java library in your... 1.16.0.719 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.15.0.699 is released. Learn to use sonar-xml-parsing version 1.15.0.699 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.15.0.699 java library in your... 1.15.0.699 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.1.690 is released. Learn to use sonar-xml-parsing version 1.14.1.690 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.1.690 java library in your... 1.14.1.690 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.0.681 is released. Learn to use sonar-xml-parsing version 1.14.0.681 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.0.681 java library in your... 1.14.0.681 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.0.678 is released. Learn to use sonar-xml-parsing version 1.14.0.678 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.14.0.678 java library in your... 1.14.0.678 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.13.0.662 is released. Learn to use sonar-xml-parsing version 1.13.0.662 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.13.0.662 java library in your... 1.13.0.662 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 2.0.0.1075 is released. Learn to use sonar-xml-parsing version 2.0.0.1075 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 2.0.0.1075 java library in your... 2.0.0.1075 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 2.1.0.1111 is released. Learn to use sonar-xml-parsing version 2.1.0.1111 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 2.1.0.1111 java library in your... 2.1.0.1111 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 2.0.1.1099 is released. Learn to use sonar-xml-parsing version 2.0.1.1099 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 2.0.1.1099 java library in your... 2.0.1.1099 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.26.0.1029 is released. Learn to use sonar-xml-parsing version 1.26.0.1029 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.26.0.1029 java library... 1.26.0.1029 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Maven dependency for org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.25.0.1003 is released. Learn to use sonar-xml-parsing version 1.25.0.1003 in Maven based Java projects
; org.sonarsource.analyzer-commons - sonar-xml-parsing version 1.25.0.1003 java library... 1.25.0.1003 of sonar-xml-parsing released The developers of   org.sonarsource.analyzer-commons - sonar-xml-parsing project have released the latest
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing like i have got the xml and the xml structure is like below... can find the above xml there is no CountryCode in the second record and 4th
XML Parsing Using Sax Parser in J2ME
XML Parsing Using Sax Parser in J2ME  Hai team, I hope you, You should help me, I have trouble in Xml parsing... I have decoded value, what... to do xml parsing help me for that... Regards Alagu
complex xml parsing and storing in database - XML
complex xml parsing and storing in database  Hi Experts ,i want to parse my xml document and store it in mysql database. This is my code. How to parse this complex data. EDI_DC40 800 0000000000557748
Problem facing in SAX Parsing - XML
Problem facing in SAX Parsing  I have facing the issue in SAX Parsing like i have got the xml and the xml structure is like below... 0668420957700159USD you can find the above xml
Parsing XML using Document Builder factory - XML
Parsing XML using Document Builder factory  Hi , I am new to XML . I am trying to parse a XML file while is : For this i am using Document builder factory parser . This is required inside a STAX job.The development env
Truncating issue while parsing XML with SAX Parser.
Truncating issue while parsing XML with SAX Parser.  I am Parsing one xml file and after parsing I have to enter the data into the database using hibernate. The problem is while parsing some elements, its not getting the complete
parsing word xml file using SAX parser - XML
parsing word xml file using SAX parser  i am parsing word 2003's XML file using SAX.here my question is,i want to write some tag elements which... this xml file and write that xml part into file? can somebody
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse... saxParser = saxParserFactory.newSAXParser(); // Parsing XML Document
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse... saxParser = saxParserFactory.newSAXParser(); // Parsing XML Document
XML Parsing Error: mismatched tag. Expected: </br>. - JSP-Servlet
XML Parsing Error: mismatched tag. Expected: .  i had an error:"XML Parsing Error: mismatched tag. Expected: ." when uploading and writing them into an xml file. i use the jspBean to call the writeXML class. pls help me to solve
Parsing repeatitive xml elements using SAX Parser - XML
Parsing repeatitive xml elements using SAX Parser  Hi, I'm using SAX Parser to read an XML file. My XML file contains repeatitive tags. I'm able to retrieve all the values from the xml file. However, for repeatitive tags

Ads