Android creating and parsing XML (IV) — detailed pull method

1. Pull overview

The package related to creating XML in Android system is org.xmlpull.v1. This package not only provides XmlSerializer for creating XML, but also provides xmlpullparser for parsing XML

Instead of extracting XML events like xmlpullparser, XmlSerializer pushes them into the data stream OutputStream or writer.

XmlSerializer provides a very intuitive API, that is, using startdocument to start a document, enddocument to end a document, starttag start element, endtag end element, text to add text, etc.

To create XML in pull mode, the standard XML constructor org.xmlpull.v1.xmlserializer is applied to create XML, and org.xmlpull.v1.xmlpullparser is used to parse XML. The following contents need to be imported

org.xmlpull.v1

Effect picture of pull creating and parsing XML:

2. Pull create XML

In the pull mode, the creation of XML is implemented through the XmlSerializer class

First, get the instance XmlSerializer that creates XML through XmlSerializer

Then, set the output through XmlSerializer, xmlserializer.setoutput, xmlserializer.startdocument ("UTF-8", null), set the XML attribute, and so on

Then, create startdocument, starttag, text, endtag, enddocument, and so on through XmlSerializer

Operation results:

3. Pull parsing XML

In the pull mode, parsing XML is implemented through the xmlpullparser class

First, get the instance XPP of parsing XML through xmlpullparser

Then, set the input XPP. Setinput (is, "UTF-8") through XPP to declare and define the data structure (such as person array) for storing XML information

Then, start is parsed through XPP_ DOCUMENT、START_ TAG、TEXT、END_ TAG、END_ Document, etc

Operation results:

4. Person class

Please refer to the previous blog Android creating and parsing XML (II) - dom method [4. Person class]

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>