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
Related Tutorials/Questions & Answers:
XML parsing using Java - XMLXML 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
Advertisements
java with xml parsing - Java Beginnersjava 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 Beginnersjava 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 - XMLparsing 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 parsingxml 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 parsingXml parsing Hi..i tried the above code but I unable to know where can i place the
xml file
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 databaseXML 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 using saxparsing 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 J2MEXML 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 charactersproblems 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 serversideXML 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
Problem facing in SAX Parsing - XMLProblem 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 J2MEXML 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 - XMLcomplex
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 - XMLProblem 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 - XMLParsing 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 - XMLparsing 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 SAXJava 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 DOMJava 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