Learn Android XML and JSON parsing together

In the network, data interaction is usually carried out in XML and JSON formats, so parsing the data in these two formats is a necessary function in Android development. This paper briefly describes the common methods of XML and JSON parsing in Android development with a simple small example, which is only for learning and sharing.

Android provides three ways to parse XML: sax (simple API XML), DOM (document object model) and pull. This article mainly explains how to parse XML by pull.

Advantages of pull parsing XML: the pull parser is small and light, fast and easy to use. It is very suitable for Android mobile devices. The pull parser is also used to parse various XML in the Android system. Android officials recommend developers to use the pull parsing technology. Pull parsing technology is an open source technology developed by a third party. It can also be applied to Java se development.

XML storage relative path: demoxml \ app \ SRC \ main \ res \ XML \ test.xml [XML folder]

If the XML file is too large, it is not suitable to execute in the main thread of activity. This article is executed in the worker thread, as shown below:

JSON is a lightweight data exchange format with good readability and easy to write quickly. The mainstream technology in the industry provides a complete solution for it, so that it can exchange data between different platforms.

Relative path of JSON storage: demoxml \ app \ SRC \ main \ res \ raw \ test2.json [raw folder]

Similarly, if the JSON file is large or the parsing is slow, it cannot be executed in the main thread of activity. A new worker thread needs to be started and executed in the background, as shown below:

If you need to deserialize JSON into class objects, or serialize class objects into JSON format files, the following is a help class:

Thousands of sails pass by the side of the sunken boat, and thousands of trees spring in front of the diseased tree.

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