XML parsing-the case of sax parsing (1) reading the contact. xml file, complete output of the document content

Source: Internet
Author: User
This article describes the XML parsing of the sax parsing case (1) Read the contact. xml file, complete output of the document content details, if necessary, refer to the next, create a Demo2 class:


Import java. io. file; import javax. xml. parsers. SAXParser; import javax. xml. parsers. SAXParserFactory;/*** read contact. xml file, complete output file content * @ author APPle **/public class Demo2 {public static void main (String [] args) throws Exception {// 1. create SAXParserSAXParser parser = SAXParserFactory. newInstance (). newSAXParser (); // 2. read the xml file MyDefaultHandler2 handler = new MyDefaultHandler2 (); parser. parse (new File (". /src/contact. xml "), handler); String content = handler. getContent (); System. out. println (content );}}


2. create a custom mydefaulthand2


Import org. xml. sax. attributes; import org. xml. sax. SAXException; import org. xml. sax. helpers. defaultHandler;/*** SAX processor program * @ author APPle */public class MyDefaultHandler2 extends DefaultHandler {// store xml document information private StringBuffer sb = new StringBuffer (); // get xml information public String getContent () {return sb. toString ();}/*** start tag */@ Overridepublic void startElement (String uri, String localName, String qName, Attri Butes attributes) throws SAXException {sb. append ("<" + qName); // you can determine whether the attribute if (attributes! = Null) {for (int I = 0; I ") ;}/ *** text content */@ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {// Get the currently read text String content = new String (ch, start, length); sb. append (content);}/*** end tag */@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {sb. append ("
 ");}}

Print the output result:

     
          
   
    
Zhang San
   20        
   
    
134222223333
           
   
    
Zhangsan@qq.com
           
   
    
432221111
       
      
          
   
    
Eric
   20        
   
    
134222225555
           
   
    
Lisi@qq.com
           
   
    
432222222
       
  
 

The above is the case of the sax parsing of XML parsing (1) Read the contact. xml file and completely output the content of the document. For more information, see The PHP Chinese website (www.php1.cn )!

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.