Parsing repeatitive xml elements using SAX Parser

Ads
 

Parsing repeatitive xml elements using SAX Parser

View Answers

April 3, 2009 at 4:06 PM

Hi Shailendra,

Use the following code to retrieve all the values from the xml file:

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

public class XMLParser{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter XML file name:");
String xmlFile = bf.readLine();
XMLParser detail = new XMLParser(xmlFile);
}
public XMLParser(String str){
try{
File file = new File(str);
if (file.exists()){
SAXParserFactory parserFact = SAXParserFactory.newInstance();
SAXParser parser = parserFact.newSAXParser();
System.out.println("XML Data: ");
DefaultHandler dHandler = new DefaultHandler(){
boolean name;
boolean age;
public void startElement(String uri, String localName,
String element_name, Attributes attributes)throws SAXException{
if (element_name.equals("Name")){
name = true;
}
if (element_name.equals("age")){
age = true;
}
}
public void characters(char[] ch, int start, int len) throws SAXException{
String str = new String (ch, start, len);
if (name){
System.out.println("Name: "+str);
name = false;
}
if (age){
System.out.println("age: "+str);
age = false;
}
}
};
parser.parse(str, dHandler);
}
else{
System.out.println("File not found!");
}
}
catch (Exception e){
System.out.println("XML File hasn't any elements");
e.printStackTrace();
}
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
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
Java XML Parsing Using SAX
Java XML Parsing Using SAX To Parse XML document using SAX parser method you.... At first Create the SAX Parser as // getting SAXParserFactory instance... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method
Read Complex XML using DOM/SAX Parser.
Read Complex XML using DOM/SAX Parser.  I have a XML file which is having two types of Items. I want to extract all details of a type only. My XML goes something like this <Movie> <Bollywood> <Actor> Shah Rukh
sax parser for xml
sax parser for xml  sax parser code that works with any XML i.e independent of any XML to extract data from 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... tag. so we were using the sax parcing in our application. can you please let me
SAX Parser for huge XML file
SAX Parser for huge XML file  Hi.... if the XML file is small... using sax parser... i wanna use direct file path... not by defining each and every... using dom parser in java. import org.w3c.dom.*; import org.w3c.dom.Node; import
read xml elements
read xml elements  i want read xml data using sax parser in java. but is there any classes or methods to read xml elements
read xml elements
read xml elements  i want read xml data using sax parser in java. but is there any classes or methods to read xml elements
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
XML- SAX Parser using JAXP API
XML- SAX Parser using JAXP API           XML- SAX Parser using JAXP API..., you will learn how XML technology works with Java using different kinds of XML
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
SAX Parser
SAX Parser       The Simple API for XML (SAX) is a serial access parser API for XML. It is used... executes, the SAX parser recognizes and responds to each XML structure taking
The Simple API for XML (SAX) APIs
applications to configure and obtain a SAX based parser to parse XML... The Simple API for XML (SAX) APIs       The SAX Packages: The SAX parser is defined in the following
SAX Parser exception
SAX Parser exception  I am trying to validate one xml, its giving me exception as : cvc-elt.5.2.2.1:The value '00' of element 'abcd' does not match the fixed {value constraint} '0'. Please help me asap
How to parse the attributes from XML using KXML parser in j2me
How to parse the attributes from XML using KXML parser in j2me  Dear Sir, I have doubt in How to parse the attributes from XML using KXML parser in j2me ..I parse the dge name value..but i didn't do parsing the attributes
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you.... At first Create the SAX Parser as // getting SAXParserFactory instance... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse method
xml parsing
xml parsing  my task is when i wll give java file to a program... and return the csv file that contains count of used keywords and idenfiers by using 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
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 xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
How to parse the attributes values from XML using KXML parser in j2me
How to parse the attributes values from XML using KXML parser in j2me  hai team, i did xml parsing from serverside...i got a resultant value from xml,but here tag value name only came from parsing attribute name cant come
parsing xml with jquery
parsing xml with jquery  Please explain how to parse a xml file into jquery. Thanks
Reading xml file using dom parser in java with out using getelementby tag name
Reading xml file using dom parser in java with out using getelementby tag name  Hi, How to read the xml file using java with dom parser, but without using getelementbytag name, and also read the attribute values also. I had
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 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
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
Read XML using Java
/xmlParsing/java-xml-parsing-using-sax.shtml http://www.roseindia.net/xml/sax...Read XML using Java  Hi All, Good Morning, I have been working... of all i need to read xml using java . i did good research in google and came to know
JavaScript XML Parser
to access and manipulate XML document using DOM parser in javascript. XML parser... JavaScript XML Parser    ... the values from the elements of XML document. The ActiveXObject("

Ads