Java parses xml using sax

Source: Internet
Author: User

A blog I wrote some time ago was about parsing xml. I used the xml parsed by dom. If the xml is not too big, there is no problem in parsing it, however, I need to parse an xml file of more than 800 KB. When the project starts running, it will become stuck and the project cannot run normally! After reading a large amount of information yesterday, I initially thought that multithreading could be used to solve this problem. Later, I passed the test! Another assumption is that there is a problem with the xml parsing method. In short, it can run normally when several pieces of data are parsed, however, if a large amount of data (the number of nodes with more than 6000 pieces of data in my file is not counted) is parsed, memory problems will occur! The last thing is stuck! Later, I followed the second solution and finally got the result. That is, I used SAX to parse xml. If I could use dom to process small data, I would need to use SAX to process a large amount of data! The following describes how to parse xml using sax.

Public class ReadXmlSAX {/*** @ param args * @ throws SAXException * @ throws ParserConfigurationException */public static void main (String [] args) throws ParserConfigurationException, SAXException {String a ="
 
 
  
"+"
  
   
True
  
  2014-03-10T18: 10: 24.9960547 + 08: 00 "+"
 "; // Xml data to be parsed (make sure the format is correct) try {SAXParserFactory factory = SAXParserFactory. newInstance (); // get the SAXParser saxParser = factory. newSAXParser (); // create the parser DefaultHandler handler = new DefaultHandler () {// declare whether these fields exist boolean bsuccess = false; boolean btime = false; boolean bindex = false; boolean bkey = false; boolean bvalue = false; public void startElement (String uri, String localName, String qName, Attributes attributes) // determine whether the tag name is the same as that of throws SAXException {System. out. println ("Start Element:" + qName); if (qName. inclusignorecase ("Success") {bsuccess = true;} if (qName. equalsIgnoreCase ("Time") {btime = true;} if (qName. equalsIgnoreCase ("Index") {bindex = true;} if (qName. equalsIgnoreCase ("Key") {bkey = true;} if (qName. equalsIgnoreCase ("Value") {bvalue = true;} for (int I = 0; I <attributes. getLength (); I ++) {System. out. println ("attribute name:" + attributes. getQName (I); System. out. println ("attribute value:" + attributes. getValue (I) ;}}/*** when parsing XML, when an element tag is read, **/public void endElement (String uri, String localName, String qName) throws SAXException {System. out. println ("End Element:" + qName);} // determines whether the tag has a public void characters (char ch [], int start, int length) throws SAXException {if (bsuccess) {System. out. println ("success:" + new String (ch, start, length); bsuccess = false;} if (btime) {System. out. println ("time:" + new String (ch, start, length); btime = false;} if (bindex) {System. out. println ("index:" + new String (ch, start, length); bindex = false;} if (bkey) {System. out. println ("key:" + new String (ch, start, length); bkey = false;} if (bvalue) {System. out. println ("value:" + new String (ch, start, length); bvalue = false ;}}; // read InputStream is = new ByteArrayInputStream (. getBytes ("ISO-8859-1"); // converts String to InputStream saxParser. parse (is, handler); // saxParser. parse ("d :\\ sss. xml ", handler); // read from local} catch (Exception e) {e. printStackTrace ();}}}

The package to be imported is dom4j. jar.


Special thanks:

51CTO Forum-JAVA Communications: 170297048 million people

This article converting String to InputStream: http://zhoujingxian.iteye.com/blog/1682480

And a lot of blogs about the sax example !!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.