Android use of development Pull Parsing and building XML
Please respect other people's labor results, reproduced please specify the Source: Android Development using pull to parse and generate XML
First, use PullparsingXML1.PULLIntroduction
I have been in the on several ways of XML parsing article describes the use ofDOMWay,SAXWay,Jdommanner, anddom4jthe way to parseXML. In addition to using the above methods to resolveXMLfile, you can also use theAndroidsystem built -in PullParser to parseXMLfile. Pullthe way the parser runs andSAXThe parser is similar. It provides similar events, such as the start element and the end element event. UseParser.next ()you can go to the next element and trigger the corresponding event. The event is sent as a numeric code, so you can use aSwitchSelect the events you are interested in and then process them accordingly. When the element begins parsing, the callParser.nexttext ()method to get the nextTextthe value of the type element.
2. features
(1) Simple Structure : An interface, an exception, a factory composed of Pull parser.
(2) Simple to use : Pull parser has only one important method Next () , which is used to retrieve the next event. And its events are only 5 .
? START DOCUMENT;
? start_tag;
? TEXT;
? end_tag;
? end_document.
(3) Multi-functional : General-purpose XML parsers and multiple implementations, and are extensible.
(4) Minimum Requirements : In order to Javame designed to work on small devices and allow for the creation of very small memory-intensive Xmlpull parser.
3. Working principle
parsing XML content in the same way SAX are similar, and also include the start element and the end element event, using Parser.next () you can go to the next element and trigger the corresponding event. But they are different,the event driver ofSAX is callback corresponding method, need to provide the method of callback, and then automatically call the corresponding method inside SAX. The pull parser does not require a method to provide a trigger. Because it triggers an event that is not a method, but a number. As for the triggering event, it is up to the programmer to decide what to do.
see here, the reader should understand why Pull The parser will be integrated Android in it. The Pull parser can also be used in non- Android projects, such as Java EE , and not necessarily only development of Android. Let's look at a sample to understand The pull parsing process.
public static list<person> getpersons (InputStream instream) throws exception{person person = null; list<person> persons = NULL; Xmlpullparser Pullparser = Xml.newpullparser (); Pullparser.setinput (instream, "UTF-8"); int event = Pullparser.geteventtype ();//Trigger first event while (event!=xmlpullparser.end_document) {switch (event) { Case XmlPullParser.START_DOCUMENT:persons = new arraylist<person> (); Break Case XmlPullParser.START_TAG:if ("person". Equals (Pullparser.getname ())) {int id = new Integer (PU Llparser.getattributevalue (0)); person = new person (); Person.setid (ID); } if (Person!=null) {if ("name". Equals (Pullparser.getname ())) {Person.setname (P Ullparser.nexttext ()); } if ("Age". Equals (Pullparser.getname ())) {Person.setage (New short (Pullparser.nexttext ()) ); }} break; Case XmlPullParser.END_TAG:if (' person '. Equals (Pullparser.getname ())) {Persons.add (person); person = null; } break; } event = Pullparser.next (); } return persons;}
Code Description:
1) int event =pullparser.geteventtype (); is pull The first event of the parser. The reader can see that the int pull The parser returns a number similar to a signal. So what do these signals mean? pull The parser has defined these five constants, and for events, only this 5
? xmlpullparser. Start_document ( start parsing, execute only once);
? xmlpullparser. Start_tag ( starting element );
? xmlpullparser. Text ( parsed);
? xmlpullparser end_tag ( end element);
? xmlpullparser end_document ( ends parsing, executes only once).
2)"parser.geteventtype ()" triggers the first event, based on The syntax of the XML, That is, it starts with parsing the document. So how do you trigger the next event? the most important way to get through the Parser :
Parser.next ();
Note: This method has a return value, in the Pull when the next event is triggered, the "signal" of the event is also obtained. The switch operation is performed by the obtained signal .
3)"parsergetattribiitevalue ()" Gets the value of the corresponding property. It has two forms, which can be indexed by attributes , or by (namespace, property name).
Second, use PullBuildXML
public static void Save (list<person> persons, OutputStream outstream) throws exception{ XmlSerializer Serializer = Xml.newserializer (); Serializer.setoutput (OutStream, "UTF-8"); Serializer.startdocument ("UTF-8", true); Serializer.starttag (NULL, "persons"); for (person person:persons) { serializer.starttag (null, ' person '); Serializer.attribute (NULL, "id", Person.getid (). toString ()); Serializer.starttag (NULL, "name"); Serializer.text (Person.getname ()); Serializer.endtag (NULL, "name"); Serializer.starttag (NULL, "age"); Serializer.text (Person.getage (). toString ()); Serializer.endtag (NULL, "age"); Serializer.endtag (null, "person"); } Serializer.endtag (NULL, "persons"); Serializer.enddocument (); Outstream.flush (); Outstream.close ();}
Description:
(1) "xmlserializctserializer= xml.newserializer ()" defines an interface to implement Xml serialization of information. What about serialization? It is also called the serialization of objects, and it is not just simply storing objects in memory , it also allows us to transfer objects over the network in a binary way so that objects can be passed like basic data. you can then re-construct an object with the same state as the original object from these contiguous byte (byte) data by crossdress.
(2) define a method, the first parameter is to generate XML the contents of the file, the second parameter is a writer. the Write ( writer) interface is more flexible than the output stream. It can be more media to elm out, such as to the hard disk, memory, network and other output. "New" a persisted XML object. Here, you should understand why to persist, it is a kind of writing to the hard disk behavior.
Serializer.setoutput (writer);
sets the direction of the output. It receives two types of output, namely the output stream and the writer. This uses the writer writer, which has just been introduced to the reader .
(3) XML in the label of the pair appear, there is a beginning, there must be an end. Therefore, when writing a satrttag () , I am accustomed to write to the corresponding endtag (). This does not make the XML generation structure a mistake when it comes to complex build structures .
method Description: The first number of parameters is XML namespace, the second is the label name, Endtag () same.
third, application examples:
Android Development parsing XML and achieving three-level linkage effect
Android Development uses pull to parse and generate XML