Detailed explanation of common methods of creating and parsing XML by Android programming

This article describes the common methods of creating and parsing XML by Android programming. Share with you for your reference, as follows:

Today, I'd like to explain some common methods for Android to create XML and parse XML.

The first is creation. We use the class XmlSerializer to create an XML file. The second is to parse XML files. The commonly used methods include DOM, sax, xmlpullparser and so on. Because the Sax code is a little complex, this section only explains Dom and xmlpullparser parsing. Sax will be explained separately in the next section. I won't talk about the advantages and disadvantages of several XML parsing methods.

In order to facilitate understanding, I made a simple demo. First, there are three buttons in the first interface. Clicking the first button will create a books.xml file in the sdcard directory. The other two buttons are to call Dom and xmlpullparser methods to parse XML files and display the results in a textview. You can follow my steps step by step:

Step 1: create a new Android project named xmldemo

Step 2: modify the main.xml layout file. The code is as follows:

Step 3: modify the main core program xmldemo.java. The code is as follows:

Step 4: since we have created a new file on SD card, we need to add permissions, as shown in the following code (line 13):

Step 5: run the above project to check the effect:

Start the first interface:

Click the create XML file button to generate the books.xml file

The contents of books.xml are as follows:

Click the DOM parse XML button:

Click xmlpullparse to parse XML:

OK ~ that's all for today.

PS: here are some online tools on XML operation for your reference:

Online XML / JSON conversion tool: http://tools.jb51.net/code/xmljson

Format XML online / compress XML online: http://tools.jb51.net/code/xmlformat

XML online compression / formatting tool: http://tools.jb51.net/code/xml_ format_ compress

XML code online formatting and beautification tool: http://tools.jb51.net/code/xmlcodeformat

More readers interested in Android related content can view the special topics of this site: summary of Android operation XML data skills, summary of activity operation skills of Android programming, summary of Android resource operation skills, summary of Android file operation skills, introduction and advanced tutorial of Android development, summary of Android view skills, and Android control usage summary

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