PULL parses XML files and pull parses xml files

Source: Internet
Author: User

PULL parses XML files and pull parses xml files


<? Xml version = "1.0" encoding = "UTF-8"?> <Students> <student id = "1001"> <stuno> 1001 </stuno> <name> Zhang San </name> <sex> male </sex> <grade> Java 1 class </grade> </student> <student id = "1002"> <stuno> 1002 </stuno> <name> Li Si </name> <sex> female </sex> <grade> Java class 2 </grade> </student> <student id = "1003"> <stuno> 1003 </stuno> <name> Level 2 </name> <sex> male </sex> <grade> Java class 3 </grade> </student> <student id = "1004"> <stuno> 1004 </stuno> <name> wax </name> <sex> female </sex> <grade> Java class 4 </grade> </student> </students>

  

1 package xmlpulldemo; 2 3 import java. io. fileInputStream; 4 import java. io. IOException; 5 6 import org. xmlpull. v1.XmlPullParser; 7 import org. xmlpull. v1.XmlPullParserException; 8 import org. xmlpull. v1.XmlPullParserFactory; 9 10 public class XmlPullDemo {11 12 public static void main (String [] args) throws XmlPullParserException, IOException {13 14 // create a parsing factory 15 XmlPullParserFactory factory = XmlPul LParserFactory. newInstance (); 16 // The factory creates a parser object 17 XmlPullParser parser = factory. newPullParser (); 18 // open the input stream corresponding to the xml document, and fill in the xml document path 19 parser. setInput (new FileInputStream ("src/xmlwendang/student. xml ")," UTF-8 "); 20 // get Event Type 21 int eventType = parser. getEventType (); 22 System. out. println (eventType = XmlPullParser. START_DOCUMENT); 23 do {24 switch (eventType) {25 case XmlPullParser. START_DOCUMENT: 26 System. Out. println ("read start, start document! "); 27 break; 28 case XmlPullParser. START_TAG: 29 String tagName = parser. getName (); 30 if (tagName. equals ("student") {31 System. out. println ("read progress-start tag:" + parser. getName () + ":" + parser. getAttributeName (0) + ":" 32 + parser. getAttributeValue (0); 33} else if (tagName. equals ("stuno") {34 System. out. println ("read progress-student ID:" + parser. nextText (); 35} else if (tagName. equals ("name") {36 System. out. println ("read Progress-name: "+ parser. nextText (); 37} else if (tagName. equals ("sex") {38 System. out. println ("read progress-Gender:" + parser. nextText (); 39} else if (tagName. equals ("grade") {40 System. out. println ("read progress-class:" + parser. nextText (); 41} else if (tagName. equals ("students") {42 System. out. println ("read start-start root directory:" + parser. getName (); 43} 44 break; 45 case XmlPullParser. END_TAG: 46 if (parser. getName (). equals ("student ")) {47 System. out. println ("read progress-end tag:" + parser. getName (); 48} else if (parser. getName (). equals ("students") {49 System. out. println ("read complete-end root directory:" + parser. getName (); 50} 51 break; 52} 53 // read the next node 54 eventType = parser. next (); 55} while (eventType! = XmlPullParser. END_DOCUMENT); 56 System. out. println ("read complete, document ended! "); 57} 58 59}

Result:

1 true 2 read start, start document! 3 read start-start root directory: students 4 read progress-start tag: student: id: 1001 5 read progress-student id: 1001 6 read progress-Name: michael Jacob 7 read progress-Gender: male 8 read progress-class: Java class 1 9 read progress-end tag: student10 read progress-start tag: student: id: 100211 read progress-student ID: 100212 read progress-Name: Li Si 13 read progress-Gender: female 14 read progress-class: Java second class 15 read progress-end tag: student16 read progress-start tag: student: id: 100317 read progress-student id: 100318 read progress-Name: xiao'er 19 read progress-Gender: male 20 read progress-class: java class 3 21 read progress-end tag: student22 read progress-start tag: student: id: 100423 read progress-student id: 100424 read progress-Name: wax 25 read progress- Gender: female 26 read progress-class: Java class 4 27 read progress-end tag: student28 read complete-end root directory: students29 read complete, document ended!

 

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.