Simple Java parsing XML using sax

Source: Internet
Author: User

1. Create a new Saxtest class, Inherit the import Org.xml.sax.helpers.DefaultHandler class

1  PackageCom.qiao.SrpingSource;2 3 Importorg.xml.sax.Attributes;4 Importorg.xml.sax.SAXException;5 ImportOrg.xml.sax.helpers.DefaultHandler;6 7  Public classSaxtestextendsDefaultHandler {8 9     PrivateString Currenttag;Ten      One     /** A * Start reading documents, usually do some initialization work -      */ - @Override the      Public voidStartdocument ()throwssaxexception { -SYSTEM.OUT.PRINTLN ("----Start Document---"); -     } -  +     /** - * Start reading nodes +      */ A @Override at      Public voidstartelement (String uri, String localname, String qName, -Attributes Attributes)throwssaxexception { -  -System.out.println (qname+ "--"); -currenttag=QName; -         if(Qname.equals ("name")) { in             //read the value in the property -             if(Attributes! =NULL) { to                  for(inti = 0; I < attributes.getlength (); i++) { +SYSTEM.OUT.PRINTLN ("SSS" +attributes.getqname (i) + "" +Attributes.getvalue (i)); -                 } the             } *         } $ Panax Notoginseng     } -  the     /** + * Read the text document in the node A      */ the @Override +      Public voidCharacters (Char[] ch,intStartintlength) -             throwssaxexception { $         //gets the contents of its node when the current node is called name $         if(Currenttag.equals ("name")){ -String content =NewString (ch,start,length); - System.out.println (content); the              -         }Wuyi  the     } -  Wu     /** - * At the end of the Read node About      */ $ @Override -      Public voidendElement (String uri, String localname, string qName) -             throwssaxexception { -     } A  +     /** the * When reading the document introduction -      */ $ @Override the      Public voidEnddocument ()throwssaxexception { the  theSystem.out.println (">> enddocument <<"); the  -     } in}

2.xml file is

  

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <class>3     <StuID= "001">4         <name>Allen</name>5         <Sex>Man</Sex>6         < Age>20</ Age>7     </Stu>8     <StuID= "002">9         <name>Namy</name>Ten         <Sex>Woman</Sex> One         < Age>18</ Age> A     </Stu> -     <StuID= "003"> -         <name>Lufy</name> the         <Sex>Man</Sex> -         < Age>18</ Age> -     </Stu> - </class>

3. Calling methods

  

1 @Test2      Public voidTest ()throwsparserconfigurationexception, saxexception{3         4SAXParserFactory factory =saxparserfactory.newinstance ();5SAXParser parser =Factory.newsaxparser ();6         7Saxtest handler =Newsaxtest ();8         9         Try {TenParser.parse (NewFile ("D:/eclipse/workspace/springsource/src/main/resource/test.xml"), handler); One              A}Catch(IOException e) { - e.printstacktrace (); -         } the          -}

4. Running Results

----Start Document---
Class--
Stu--
Name--
Allen//The value of the name node read to


Sex--
Age--
Stu--
Name--
Namy//The value of the name node read to


Sex--
Age--
Stu--
Name--
Lufy//The value of the name node read to


Sex--
Age--
>> enddocument <<

Simple Java parsing XML using sax

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.