Home Answers Viewqa XML Parsing repeatitive xml elements using SAX Parser

 
 


shailendra
Parsing repeatitive xml elements using SAX Parser
1 Answer(s)      4 years and 2 months ago
Posted in : XML

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 Pages:
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
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
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
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
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
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
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
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
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
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
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
parser - XML
parser  how to convert XML file to flat file(text file) using DOM and SAX parser
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
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
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
XML Count Elements
; that are used in your XML document using the SAX parser.  Description of program... XML Count Elements      ...) that contains xml file. We define a default handler that implements for all SAX
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
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
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
Java SAX Examples
_Id> that are used in your XML document using the SAX parserXML... and column number) the generated events  while parsing a XML file using SAX APIs... From the XML File Here you will learn to retrieve data from XML file using SAX
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
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("
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 Parsers
; XML parser is used to read, update, create and manipulate an XML document. Parsing... and manipulate XML documents using any programming language (and a parser... stored in XML documents using any programming language (and a parser
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
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
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
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
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 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
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
Handling Errors While Parsing an XML File
and obtain a SAX based parser to parse XML documents.SAXParser parser... Handling Errors While Parsing an XML File   ... parsing an XML document. JAXP (Java API for XML Processing) is an interface
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
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
Locating the Notified Events
) the generated events  while parsing a XML file using SAX APIs.  Description of program: This program takes a XML file at the console. Before parsing... a content event handler. The parser starts parsing a XML document, as soon as 
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 XML parser
Java XML parser  Hi friends, i am new to java XML parsing. i need to parse the following xml response which i got from a url hit: <response> <status>SUCCESS</status> <app-config> <CHECK_LOW_SCORE>
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 using java code
parsing xml file using java code  parsing a xml file using java code
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... the XML file.  After parsing the XML file a NodeList is created using
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
java - XML
java  Sir, I would like to add attribute to all the elements in the XML document while i am parsing the file using SAX event based parser... parser = parserFact.newSAXParser(); System.out.println("XML Data
Xml Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file? Thanks
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 XML
using JDOM | Parsing The XML File Using JDOM Parser in JSP... instead it uses the callback methods for parsing the XML document. The SAX... program you can use any of the available XML parsing API and then process
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.