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



Home Answers Viewqa XML Parsing repeatitive xml elements using SAX Parser
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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



Advertisement
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 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
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 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
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 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
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
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- 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
parser - XML
parser  how to convert XML file to flat file(text file) using DOM and SAX parser
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
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 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
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
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
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
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
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
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 Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file? Thanks
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
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
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
 

 

 

DMCA.com