parsing XML file to get java object

parsing XML file to get java object

View Answers

April 28, 2009 at 2:34 PM

Hi Friend,

Use the following code:

import java.io.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;

public class RetrieveData {
public static void main(String[] args) {
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a xml file name: ");
String xmlFile = bf.readLine();
File file = new File(xmlFile);
if (file.exists()){
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
SaxHandler handler = new SaxHandler();
parser.parse(xmlFile, handler);
}
else{
System.out.println("File not found!");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private static class SaxHandler extends DefaultHandler {
public void startElement(String uri, String localName, String qName,
Attributes attrs) throws SAXParseException,SAXException {
boolean row;
if (qName.equals("row")){
row = true;
int length = attrs.getLength();
for (int i=0; i<length; i++) {
String name = attrs.getQName(i);
System.out.print(name);
String value = attrs.getValue(i);
System.out.println("\t"+value);
}
}
}
}
}
Thanks









Related Tutorials/Questions & Answers:
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
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
Advertisements
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
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
Java get XML File
Java get XML File    ... the content of 'employee.xml' file as an XML document an return the DOM object... the XML file. For this, you need to create a XML file. Here is the employee.xml
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 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
JAXB Create XML File And Get Data From XML
how to create XML file and how to convert XML file's data to Java Object... an XML file using Java Object then we will read that XML data as Java Object... WriteToXmlFile.java for writing the Java object into XML file. In this class we will use
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
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 and return the csv file that contains count of used keywords and idenfiers by using
Xml parsing
Xml parsing  Hi..i tried the above code but I unable to know where can i place the xml file
parsing xml with jquery
parsing xml with jquery  Please explain how to parse a xml file into jquery. Thanks
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 using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
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 its methods and for each method to get its parameters. I'm trying to do
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
Parsing into date object
Parsing into date object  Here is my code: String de = (String...(diff); **I tried this to get the difference between the two dates. But, it is showing error at the line where i performed parsing operation. Please help me
Parsing into date object
Parsing into date object  Here is my code: String de = (String...(diff); **I tried this to get the difference between the two dates. But, it is showing error at the line where i performed parsing operation. Please help me
Handling Errors While Parsing an XML File
Handling Errors While Parsing an XML File  ... parsing an XML document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you need to follow the following steps. Consider the following XML file... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml
Java error reached end of file while parsing
Java error reached end of file while parsing... an error i.e java error reached end of file while parsing as the class closing braces... of file while parsing ,In this code we want to display Time in hour, minute
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
Ignoring Comments While Parsing an XML File
Ignoring Comments While Parsing an XML File  ... in an XML File. JAXP is an interface which provides parsing of xml documents. Here... a DocumentBuilder object with the help of a DocumentBuilderFactoryFile file = new File
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
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
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...-xml-parsing version 1.28.0.1058 in your pom.xml file you can run following... 1.28.0.1058 of sonar-xml-parsing released The developers of 
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.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.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.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.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 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...-xml-parsing version 1.26.0.1029 in your pom.xml file you can run following... 1.26.0.1029 of sonar-xml-parsing released The developers of 
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.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...-xml-parsing version 1.27.0.1040 in your pom.xml file you can run following... 1.27.0.1040 of sonar-xml-parsing released The developers of 
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.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.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.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 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...-xml-parsing version 1.25.0.1003 in your pom.xml file you can run following... 1.25.0.1003 of sonar-xml-parsing released The developers of 

Ads