XML Overview and Pull parsing details ONEGoal and ONEPassion! Json is the most widely used for Android development, and Xstream is used for xml parsing. slowly, manual xml parsing is almost forgotten. Overview: What is XML? EXtensibleMarkupLanguage
XML Overview and Pull parsing
ONE Goal ,ONE Passion !
Most Android developers use json. in addition, Xstream is used for parsing xml, and the manual parsing of xml is almost forgotten. in general, the most common demo4j is based on dom, and the pull parsing based on sax. another way is to forget it.
Overview:
What is XML?
EXtensible Markup Language (eXtensible Markup Language );
Common functions of XML:
1. used as the configuration file
2. data format during data transmission
3. Android Chinese source files
Basic XML syntax:
1. The statement must be written in the first line.
Version: xml version number. Currently, only version 1 and 0 encoding: encoding format
2. only one root tag is allowed.
3. each label must be closed.
4. Cross-nesting is not allowed.
Shape:
#3F51B5
5. CDATA zone
将有特殊符号文本显示 if( 3< 5 ){ }
For example, CDATA must be used when special symbols such as "<", ">" are used in the document. otherwise, compilation fails.
Compile an XML file using Pull.
Public class WriteXml {public static void main (String [] args) throws Exception {// Create an xml parsing factory XmlPullParserFactory factory = XmlPullParserFactory. newInstance (); // Create a serializer (xml generator) XmlSerializer ser = factory through the factory. newSerializer (); // specify the output stream for the serializer (write the xml file to a specified file) ser. setOutput (new FileOutputStream ("src/B. xml ")," UTF-8 "); // start to write the xml file // 1. xml declaration ----------
Ser. startDocument ("UTF-8", true); // 2. start tag ----------
<书库>
Ser. startTag (null, ""); for (int I = 0; I <2; I ++) {// 3. start tag -------------
<书>
Ser. startTag (null, "book"); // 4. start tag ------------
<书名>
Ser. startTag (null, "title"); // 4.1 sets attributes for the title tag ---------
<书名 id="1001">
Ser. attribute (null, "id", "1001"); // 4.2 label setting text ------------
<书名 id="1001">
Centennial lonely ser. text ("Centennial lonely"); // 5, end label ------------
Ser. endTag (null, "name"); // 6. end tag ------------
Ser. endTag (null, "book");} // 7. end of root tag -----------
Ser. endTag (null, ""); ser. endDocument ();}}
The generated B. xml file is:
<书库>
<书>
<书名 id="1001">
Hundred years of loneliness
<书>
<书名 id="1002">
Hundred years of loneliness
Pull parses XMl:
Public class XmlParserDemo {/*** @ param args * @ throws Exception */public static void main (String [] args) throws Exception {// create the pull parser factory object XmlPullParserFactory factory = XmlPullParserFactory. newInstance (); // create the parser object ArrayList
List = null; Book book = null; // Get the parser XmlPullParser parser = factory. newPullParser (); // parse xml parser from the specified file. setInput (new FileInputStream ("src/B. xml ")," UTF-8 "); // 1. determine whether it is the end tag of the root tag while (parser. getEventType ()! = XmlPullParser. END_DOCUMENT) {switch (parser. getEventType () {case XmlPullParser. START_TAG: // start tag if (parser. getName (). equalsIgnoreCase (" ")){//------------
<书库>
// If it is a tag, create a set storage object list = new ArrayList
();} Else if (parser. getName (). equalsIgnoreCase (" ")){//----------
<书>
// When the tag is an object, create the object book = new Book ();} else if (parser. getName (). inclusignorecase ("name ")){//----------
<书名>
// Obtain the attributes and text in the current tag. and stored in the object String id = parser. getAttributeValue (null, "id"); book. setId (id); String name = parser. nextText (); book. setName (name);} break; case XmlPullParser. END_TAG: // end tag // when the tag is the end tag of the object, the object is stored in the set
If (parser. getName (). inclusignorecase (" ")){//-------// Note: At that timeYou do not need to process the list when the tag ends. add (book);} break;} parser. next () ;}for (int I = 0; I <list. size (); I ++) {System. out. println ("---" + list. get (I ));}}}
Note: 1. use the kxml2-2.3.0.jar and xmlpull_1_1_3_4c.jar to parse the jar.
If no build. path exists, the following error is reported:
org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
2. return value of parser. getName () method-tag name
Shape:
// Return null
// Return the title, null, title
<书名 id="1001">
Hundred years of loneliness
// Return the title, null, title
<书名 id="1002">
When parser is parsed,. parser will also be moved to the text of the tag question. text has no tag name, so it is null.
The above is the detailed content of the XML Overview and sample code for Pull parsing. For more information, see other related articles in the first PHP community!