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



Home Answers Viewqa XML SAX Parser for huge XML file
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Nayana NAnjundappa
SAX Parser for huge XML file
2 Answer(s)      2 years and 4 months ago
Posted in : XML


Hi.... if the XML file is small and repetitive then this will have meaning.. import javax.xml.parsers.; import org.xml.sax.; import org.xml.sax.helpers.; import java.io.;

public class SAXElementCount{  
  int startTag = 0;
  public static String ele;
  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();
  File file = new File(xmlFile);
  if (file.exists()){
  System.out.print("Enter XML tag name:");
  ele = bf.readLine();
  SAXElementCount tagCount = new SAXElementCount(xmlFile);
  }
  else{
  System.out.println("File not found!");
  }
  }
  public SAXElementCount(String str){
  try{
  SAXParserFactory parserFact = SAXParserFactory.newInstance();
  SAXParser parser = parserFact.newSAXParser();
  DefaultHandler dHandler = new DefaultHandler(){
  public void startElement(String uri, String name, String element, 
    Attributes atri)throws SAXException{
  if (element.equals(ele)){
  startTag++;
  }
  }
  public void endDocument(){
  System.out.println("Total elements: " + startTag);
  }
  };
  parser.parse(str,dHandler);
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
}

..

What if the xml file is very big or huge file? and sometimes we wont be aware of type of xml file...


Advertisement
View Answers

July 20, 2012 at 5:20 PM


Here is a code that parse xml file using dom parser in java.

import org.w3c.dom.*;
import org.w3c.dom.Node;
import javax.xml.parsers.*;
public class ParseXML{
    public static boolean isTextNode(Node n){
    return n.getNodeName().equals("#text");
  }
    public static void main(String[]args)throws Exception{
  DocumentBuilderFactory docFactory =  DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  Document doc = docBuilder.parse("c:/file.xml");

          Element  element = doc.getDocumentElement(); 
          NodeList personNodes = element.getChildNodes();     
          for(int i=0; i<personNodes.getLength(); i++){
            Node emp = personNodes.item(i);
            if(isTextNode(emp))
            continue;
            NodeList nlist = emp.getChildNodes(); 

            for(int j=0; j<nlist.getLength(); j++ ){
            Node node = nlist.item(j);

            if(isTextNode(node)) 
              continue;

            System.out.print(node.getFirstChild().getNodeValue()+"\t ");
          } 
          System.out.println();
        }
    }
}


July 20, 2012 at 5:23 PM


I need to parse using sax parser... i wanna use direct file path... not by defining each and every element..

Thanks



Related Tutorials/Questions & Answers:
SAX Parser for huge XML file
SAX Parser for huge XML file  Hi.... if the XML file is small...(); } } } .. What if the xml file is very big or huge file? and sometimes we wont... using sax parser... i wanna use direct file path... not by defining each and every
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
Advertisements
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 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
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... could understand was while calling the event handler of the SAX Parser i.e.
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
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... that value append to send web service and got reult...here i got xml feed now i want
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 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
parser - XML
parser  how to convert XML file to flat file(text file) using DOM and SAX parser
Xml Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file? Thanks
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.... At first Create the SAX Parser as // getting SAXParserFactory instance
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
XML- SAX Parser using JAXP API
XML- SAX Parser using JAXP API           XML- SAX Parser using JAXP API In the previous issue of Java Jazz Up you have read about XML technology. In this issue
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... of program: In this example you need a well-formed XML file that has some data (Emp
The Simple API for XML (SAX) APIs
applications to configure and obtain a SAX based parser to parse XML... to the parser. This parser processes the XML file and invokes the appropriate method... The Simple API for XML (SAX) APIs   
Problem facing in SAX Parsing - XML
of all remove & sign from your xml file,it contradicts.And then try...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
parsing xml using sax
parsing xml using sax  how to get values if the root elements are same and their attributes are different
parser xml one page to another
parser xml one page to another  parser xml one page to another
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...-Beginners/8384-java-with-xml-parsing.html but it is not reading attribute
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... 0668420957700159USD you can find the above xml
JavaScript XML Parser
JavaScript XML Parser    ... to access and manipulate XML document using DOM parser in javascript. XML parser.... The method xmlDoc.load("data.xml") allows the parser to load an XML
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>
Parsing The XML File Using JDOM Parser in JSP
Parsing The XML File Using JDOM Parser in JSP... with JDOM parser to parse the xml document. JDOM can read existing XML documents... native parser. XML parsing produces an xml parse tree from an XML document. Using
The Simple API for XML (SAX) APIs
to configure and obtain a SAX based parser to parse XML documents.... This parser processes the XML file and invokes the appropriate method on the handler... The Simple API for XML (SAX) APIs   
XML Count Elements
; that are used in your XML document using the SAX parser.  Description of program...) that contains xml file. We define a default handler that implements for all SAX... this program takes a xml file and checks it. If the given file exists, again
Parsing The XML File using DOM parser in JSP
Parsing The XML File using DOM parser in JSP... the xml page in jsp. 'parsingxml.jsp' uses 'roseindia.xml' file and display its... the DOM (Document Object Model) parser, that is the platform independent
Insert XML file data to database
Insert XML file data to database In this tutorial, you will learn how to insert the xml file data to database using dom parser. You all are aware of XML... to read the xml file data and save the values of an XML file to a Database
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 modification
Java XML modification This section explain you how to modify the xml file using DOM parser. For this purpose, you have to use DocumentBuilderFactory, DocumentBuilder and Document classes to get the default DOM parser and parse the xml
Advertisements
 

 

 

DMCA.com