Detailed explanation of XML file parsing and writing methods based on pull on Android platform

This paper describes in detail the parsing and writing methods of XML files based on pull on Android platform. Share with you for your reference, as follows:

XML technology has been widely used in cross platform data interaction. If we need to develop an Android application, we need to interact with the server. Through XML files, we can easily transmit data between Android platform and server. The specific implementation involves the technology of parsing and writing XML files. This paper implements the technology of XML file parsing based on pull on Android platform.

Xmlpullparser is an open source API package implemented in Java (source download address) http://www.xmlpull.org/ ), implements the method of parsing XML files in pull mode. The API is included in the Android SDK. When using, you need to import the following three packages:

Let's take the data class people in the classroom exercise of SQLite database as an example. The people class has four attributes: ID, name, age and height. Suppose there is a test data id = 1, name = "Du Fu", age = 30 and height = 1.75,

The corresponding data item elements in the XML file are as follows:

1. Use xmlpullparser to parse the file

First, you need to initialize an xmlpullparser object, r.xml Peopleinfo is the file peopleinfo The identification of XML in the project. After reading the file, parse the elements in the file step by step.

The related functions and descriptions of xmlpullparser are as follows:

Parsing steps:

1) Initialize the parser and specify the XML file. 2) read the document start ID, the root element < peopleinfo > 3) read the data item element start ID, < people > A) read the attributes of the data item element, tag = "Item1" B) read the sub elements of the data item, ID, name, age, height. 4) end a data item, < / people >, save the results of the data item. 5) read the document End ID, </peopleinfo>

2. Write XML files using XmlSerializer

The process of writing an XML file: first initialize an XmlSerializer object serializer, set the output writer object, then write the data to the serializer, and then write the object to the file through the writer.

The related functions and descriptions of XmlSerializer are as follows:

explain

Sets the output writer object

The initial identification statement written to the XML file must be called after setoutput

Write start element ID

Write element value

Write element end ID

Write document end tag

1) Initialize serializer 2) set writer object writer 3) write document start flag <? xml version="1.0" encoding="UTF-8"standalone="yes" ?> 4) Write root element < peopleinfo > 5) write data item element < people > A) write data item attribute tag = "people1" B) write data item sub element, ID, name, age, height 6) write data item element terminator < / people > 7) write root element terminator < / peopleinfo > 8) write document terminator 9) write data to file through writer, writer Flush () 10) closes the writer object

Implement the tool class xmlpullhelper, which provides the ability to parse and write XML files

1. The specific implementation code is as follows:

1. Use this tool class xmlpullhelper to implement

The Android SDK has a built-in pull interpreter. It is convenient to use xmlpullparser. There are many technologies for parsing XML files, such as sax and DOM, which need more learning.

More readers interested in Android related content can view the special topics of this site: summary of Android operating XML data skills, summary of Android programming activity operating skills, summary of Android resource operating skills, summary of Android file operating skills, summary of Android operating SQLite database skills, summary of Android operating JSON format data skills Summary of Android database operation skills, summary of SD card operation methods for Android programming development, introduction and advanced tutorial for Android development, summary of Android view skills and summary of Android control usage

I hope this article will help you in Android programming.

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
分享
二维码
< <上一篇
下一篇>>