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



Home Answers Viewqa XML SAX Parser for huge XML file

Have Programming Question? Ask it here!
 
 
 


Nayana NAnjundappa
SAX Parser for huge XML file
2 Answer(s)      a year and 9 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
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
parser xml one page to another
parser xml one page to another  parser xml one page to another
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
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
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
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 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
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
Java SAX Examples
From the XML File Here you will learn to retrieve data from XML file using SAX..._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
XML Parsers
; XML parser is used to read, update, create and manipulate an XML document. Parsing XML Documents To manipulate an XML document, XML parser is needed. The parser... and manipulate XML documents using any programming language (and a parser
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
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
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
Run XML file
Run XML file  Hi.. How do I execute or run an XML file? please tell me about that 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 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
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
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
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 parser
Java parser  What is the difference between DOM and SAX
 

 

 

DMCA.com