Share the sample code for parsing XML in detail

Source: Internet
Author: User
Perform Sax parsing on XML: 1. perform Sax parsing on XML: it is event-driven. install XML to parse it step by step. the advantage is that you do not need to call the entire document in advance and consume less resources. The disadvantage is that after an event, if data is not saved, the data will be lost; stateless; you can only get text from the event, but I do not know which element the text belongs. perform Sax parsing on XML:

1. perform Sax parsing on XML:

The event-driven parsing of XML by Sax is installed step by step. the advantage is that you do not need to call the entire document in advance and consume less resources. The disadvantage is that after an event, if data is not saved, the data will be lost; stateless; you can only get text from the event, but I do not know which element the text belongs.

II. implementation:

1. create an XMl document:

 
     
         
   
    scott
          
   
    scott
       
      
         
   
    sys
          
   
    sys
       
      
         
   
    system
          
   
    system
       
      
 

2. start parsing:

Package Sax parses Xml; import java. io. file; import java. io. IOException; import javax. xml. parsers. parserConfigurationException; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory; import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler; public class SaxParser {public static void main (String [] args) {// 1. instantiate the SaxParserFactory object SAXParserFac Export factory = SAXParserFactory. newInstance (); try {// 2. create a parser: SAXParser saxParser = factory. newSAXParser (); // 3. obtain the document to be parsed, generate a parser, and parse the File xmlFile = new File ("myXml \ cb. xml "); MyHandler handler = new MyHandler (); // start parsing: saxParser. parse (xmlFile, handler);} catch (ParserConfigurationException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (SAXException e) {// TODO Auto-generated catc H blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public static class MyHandler extends DefaultHandler {// used to record the name of the last node to be parsed. private String preTag = null; private boolean ifEnd = false; private String getName; private String getPwd; @ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {// TODO Auto-generated method Stub // super. characters (ch, start, length); if (preTag! = Null) {if ("name ". equals (preTag) {getName = new String (ch, start, length);} else if ("pwd ". equals (preTag) {getPwd = new String (ch, start, length); ifEnd = true; System. out. println ("
 
  
"+ GetName +"
 "); System. out. println ("
 
  
"+ GetPwd +"
 ") ;}}@ Overridepublic void endDocument () throws SAXException {// TODO Auto-generated method stub // super. endDocument (); System. out. println (""); System. out. println ("--------------------------"); System. out. println ("XML parsing completed") ;}@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {// TODO Auto-generated method stub if (ifEnd = true) {System. out. println (""); IfEnd = false;} // after a tag is parsed, set preTag to null; preTag = null;} @ Overridepublic void startDocument () throws SAXException {// TODO Auto-generated method stubSystem. out. println ("starting to parse XML files"); System. out. println ("------------------------------"); System. out. println ("
 "); System. out. println ("
 
  
") ;}@ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {// TODO Auto-generated method stub //
  
   
If ("user ". equals (qName) {String s = qName; String s1 = attributes. getValue (0); // IdString s2 = attributes. getLocalName (0); // System. out. println (s + s1 + s2); System. out. println ("<" + qName + "" + "id =" + "\" "+ s1 +" \ "" + "> ");} preTag = qName ;}}}
  
 

III. running result:

IV. Additional instructions:

1. execution sequence:

Because the Sax parsing is performed in the order of xml files The startDocument () method is called. Because it is an ElementNode, startElement (String uri, String localName, String qName, Attributes attributes) will be called. to get the information of oracle children, the characters (char [] ch, int start, int length) method is called.

2. add the static keyword to the internal class:

Internal classes are dynamic, that is, they start with public class. The main program is public static class main. In Java, static methods in classes cannot directly call dynamic methods. Only when an internal class is modified to a static class can the member variables and member methods of the class be called in the static class. Therefore, the simplest solution is to change the public class to public static class without any other changes.

3. parameters of the startElement (String uri, String localName, String qName, Attributes attributes) method:

V. Summary:

Today, I finally completed the two parsing methods for XML. at the beginning, the teacher asked me to write it. I did not write it myself. I probably wrote a Sax file. now I can't understand it. I am also drunk, using the two methods, I think Sax parsing is relatively lightweight.

The above is a detailed description of the sample code for the Sax parsing of XML. For more information, see other related articles in the first PHP community!

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.