SAX Parser for huge XML file

Ads
 

SAX Parser for huge XML file

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...

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

Ads









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
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
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
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
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
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
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
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
parser xml one page to another
parser xml one page to another  parser xml one page to another
executio of xml file
executio of xml file  how to execute xml file , on web browser
ModuleNotFoundError: No module named 'xml-parser'
ModuleNotFoundError: No module named 'xml-parser'  Hi, My Python... 'xml-parser' How to remove the ModuleNotFoundError: No module named 'xml... have to install padas library. You can install xml-parser python
ModuleNotFoundError: No module named 'xml-parser'
ModuleNotFoundError: No module named 'xml-parser'  Hi, My Python... 'xml-parser' How to remove the ModuleNotFoundError: No module named 'xml... have to install padas library. You can install xml-parser python
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
xml file creation in java
xml file creation in java  how to create xml file in java so that input should not be given from keyboard. and that file should be stored.   Please visit the following links: http://www.roseindia.net/tutorial/java/xml
How to prepare XML file?
How to prepare XML file?  Hi, I want to prepare XML File, can you... you create an XML file.. Create XML File using Servlet How to generate build.xml file Complete Hibernate 3.0 and Hibernate 4 Tutorial Thanks
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
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
retrieve in xml file in struts2
retrieve in xml file in struts2  i am using struts2 and trying... can i get back the value from the xml file in the ajax function and print... properties file using ajax. i now put the values from properties file to map
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>
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
ModuleNotFoundError: No module named 'django-rss-xml-parser'
ModuleNotFoundError: No module named 'django-rss-xml-parser'  Hi...: No module named 'django-rss-xml-parser' How to remove the ModuleNotFoundError: No module named 'django-rss-xml-parser' error? Thanks   Hi
ModuleNotFoundError: No module named 'yandex-xml-parser'
ModuleNotFoundError: No module named 'yandex-xml-parser'  Hi, My... named 'yandex-xml-parser' How to remove the ModuleNotFoundError: No module named 'yandex-xml-parser' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'yandex-xml-parser'
ModuleNotFoundError: No module named 'yandex-xml-parser'  Hi, My... named 'yandex-xml-parser' How to remove the ModuleNotFoundError: No module named 'yandex-xml-parser' error? Thanks   Hi, In your
questions from an xml file
questions from an xml file  i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one

Ads