Parsing repeatitive xml elements using SAX Parser

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









Related Tutorials/Questions & Answers:
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
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
Advertisements
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
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
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
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...;/Students> 1. At first Create the SAX Parser as // getting SAXParserFactory... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse
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
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... then i need to skip the total Customer tag. so we were using the sax parcing
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
XML parsing using Java - XML
XML parsing using Java  I'm trying to parse a big XML file in JAVA... for it skipping all the elements in Table 1. I'm usin using saxparser, startelement..." in element "MIRate"(THE XML code is below).once he enters coverage rate I need
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
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
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
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
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 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 Count Elements
; that are used in your XML document using the SAX parser.  Description of program... XML Count Elements      ... name:Employee-Detail.xml Enter XML tag name:Emp_Id Total elements: 3
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
Parsing The XML File Using JDOM Parser in JSP
Parsing The XML File Using JDOM Parser in JSP... native parser. XML parsing produces an xml parse tree from an XML document. Using... with JDOM parser to parse the xml document. JDOM can read existing XML documents
how to parse xml document which contains elements with namespace using JDOM - XML
Lucknow "parsingxml.jsp" Parsing of xml using DOM Parser...how to parse xml document which contains elements with namespace using JDOM  Hello, I am parsing XML file ,which has structure like
The Simple API for XML (SAX) APIs
to configure and obtain a SAX based parser to parse XML documents... The Simple API for XML (SAX) APIs       The SAX Packages: The SAX parser is defined in the following
parser - XML
parser  how to convert XML file to flat file(text file) using DOM and SAX parser
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
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 The XML File using DOM parser in JSP
Parsing The XML File using DOM parser in JSP... provides actions for parsing an XML document. This example helps you to parsing... the DOM (Document Object Model) parser, that is the platform independent
Java XML Parsing Using DOM
Java XML Parsing Using SAX To Parse XML document using SAX parser method you...;/Students> 1. At first Create the SAX Parser as // getting SAXParserFactory... = saxParserFactory.newSAXParser(); // Parsing XML Document by calling parse
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
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
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 in jsp
parsing xml file in jsp  example that pars XML file in JSP
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
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
Java SAX Examples
file using SAX parser. We use the JAXP APIs to retrieve data from XML document... of tags <Emp_Id> that are used in your XML document using the SAX parser... parsing a XML file using SAX APIs.      
Getting all XML Elements
elements of the XML file using the DOM APIs. This APIs provides some constructors and methods which helps us to parse the XML file and retrieve all elements... elements. Program parses the xml file using the parse() method and creates
The Simple API for XML (SAX) APIs
The Simple API for XML (SAX) APIs       The SAX Packages: The SAX parser is defined in the following... based parser to parse XML documents org.xml.sax Package : Describing few
Get Data From the XML File
Get Data From the XML File       Here you will learn to retrieve data from XML file using SAX parser. We use the JAXP APIs to retrieve data from XML document . Description
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
XML Parsers
; XML parser is used to read, update, create and manipulate an XML document. Parsing XML Documents To manipulate an XML document, XML parser is needed. The parser..., its data can be manipulated using the appropriate parser. ADS_TO_REPLACE_1
Xml Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file?ADS_TO_REPLACE_1 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
XML Elements
XML Elements       XML Elements are extensible. They have relationships. They have simple naming rules. XML Elements are Extensible XML documents can be extended to carry more
Java xml count occurrence of elements
of each element in an XML document using SAX Parser and display them. The document...Java xml count occurrence of elements Java provides xml parsers to read, update, create and manipulate an XML document in a standard way. You have used xml
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

Ads