SAX Parser for huge XML file

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









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
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
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
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
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...;/Students> 1. At first Create the SAX Parser as // getting SAXParserFactory
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
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 Parser
Xml Parser  Hi... please tell me about What parser would you use for searching a huge XML file?ADS_TO_REPLACE_1 Thanks
The Simple API for XML (SAX) APIs
to configure and obtain a SAX based parser to parse XML documents... is passed to the parser. This parser processes the XML file and invokes... 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
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:ADS_TO_REPLACE_1 In this example you need a well-formed XML file that has
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
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
The Simple API for XML (SAX) APIs
. This parser processes the XML file and invokes the appropriate method on the handler... The Simple API for XML (SAX) APIs       The SAX Packages: The SAX parser is defined in the following
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
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
XML Count Elements
; that are used in your XML document using the SAX parser.  Description of program.... When you run this program takes a xml file and checks it. If the given file... (SAXElementCount) that contains xml file. We define a default handler that implements
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
xml file display - XML
xml file display   - - - - ADL SCORM CAM 1.3 - - session3 - Online Instructional Strategies that Affect Learner... code to display the above xml file in tree structure where
parser xml one page to another
parser xml one page to another  parser xml one page to another
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
Sorting Xml file - XML
Sorting Xml file  I have an xml file like this: Smith USA... sort my xml file. Here is my xslt..., my ouput file will be like this: Amy AUC Bob USA John UK
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
splitting large xml file. - XML
splitting large xml file.  Hi, I have a large xml file(1G) and a schema (XSD) i need to split this xml to small xml files (~20M) that will be valid by the schema
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
executio of xml file
executio of xml file  how to execute xml file , on web browser
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
JavaScript XML Parser
JavaScript XML Parser    ... to access and manipulate XML document using DOM parser in javascript. XML parser reads and loads the xml into computer memory and converts it into DOM object
Version of sax>sax dependency
List of Version of sax>sax dependency
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.      
How to create XML file - XML
How to create XML file  Creating a XML file, need an example. Thanks!!  To create XML file you need plain text editor like note pad. After creating your file save it with .xml extension.  Hi,Java programming
Run XML file
Run XML file  Hi.. How do I execute or run an XML file? please tell me about thatADS_TO_REPLACE_1 Thanks
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 read this xml file - XML
how to read this xml file  i want to read this xml file using java... read i have tried lot more , but i am not able to read this xml file...[])throws Exception { File f=new File("C:/data.xml"); DocumentBuilderFactory
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 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
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
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
xml configuration file - JDBC
xml configuration file  Hi, Could you please tell me how to write a xml configuration file . We have mysql database in some other system. I have... to create a xml file. Please help me out. Thank you  Hi Friend, Try
Convert text file to XML file in Java
Convert text file to XML file Java In this section, you will learn how to convert the text file into xml file in Java language. To do this, we have used... file. The Parser invoked startElement() method at the beginning of every element

Ads