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



Home Answers Viewqa XML parsing XML file to get java object
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Phyo Phyo
parsing XML file to get java object
1 Answer(s)      5 years and 7 months ago
Posted in : XML



Advertisement
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 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 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 get XML File
Java get XML File     ... of 'employee.xml' file as an XML document an return the DOM object. getDocumentElement... the XML file. For this, you need to create a XML file. Here is the employee.xml
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
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 parsing
Xml parsing  Hi..i tried the above code but I unable to know where can i place the xml file
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
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 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
JAXB Create XML File And Get Data From XML
JAXB Create XML File And Get Data From XML In this section we will read about how to create XML file and how to convert XML file's data to Java Object... the Java object into XML file. In this class we will use the following classes
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 method
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 method
how to get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
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
java get file permission
java get file permission  How to check the get file permission in Java
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
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
In Java how to get size of class and object?
In Java how to get size of class and object?  In Java how to get size of class and object
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
How to get xml file form http port using web service
How to get xml file form http port using web service  hi I am suresh... and use the xml data to convert it to java file and store it to DB. But i am struck with getting xml file. How to access xml file in web service? Please Help
 

 

 

DMCA.com