Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners java with xml parsing
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


valarmathi
java with xml parsing
3 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners


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

Advertisement
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
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
Advertisements
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
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
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
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
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
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
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
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
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
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
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
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
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
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 method... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method of SAXParser
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 method... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method of SAXParser
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 date in Java
Parsing date in Java  How can i parse the date in Java in a simple format..?   SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy
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 parsing of data types
java parsing of data types  Why its not works? char ch; String s=* ch=Character.parseChar(s) program: char op; DataInputStream dis=new DataInputStream(System.in); System.out.print("Enter operator (+,-,*,/,%): "); op
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
How to implement xml parsing in iphone ? -RV
How to implement xml parsing in iphone ? -RV  In .h file @interface RootViewController : UITableViewController<NSXMLParserDelegate>{ IBOutlet UITableView *page1; NSMutableArray *titlearray
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
 

 

 

DMCA.com