Android read/write XML (on)

Source: Internet
Author: User

 

Note: This article from the http://www.moandroid.com. Original address: http://www.moandroid.com /? P = 508

 

XML is often used as a data format on the Internet. The file format must be clear to everyone. Here I use the Android platform to describe the package for reading and writing XML provided by the android SDK.

 

First, we will introduce the relationship between data packets between Android SDK and Java SDK in reading and writing XML files. The biggest advantage of the Android platform is that it uses the Java programming language. The android SDK does not provide all available functions to the standard Java Runtime Environment (JRE), but it supports a large part of these functions. The Java platform supports using XML in many different ways, and most XML-related Java APIs are fully supported on Android. For example, Java's simple APIs for XML (SAX) and Document Object Model (DOM) are available on Android. These APIs have been part of Java Technology for many years, the newer streaming API for XML (Stax) is not available in Android. However, Android provides a library with similar functions. Finally, the Java XML binding API is not available in Android. This API can be implemented in Android. The android SDK provides the following package to support XML read/write:

 

Package features
Javax. xml defines core XML constants and functions according to XML specifications.
Javax. xml. parsers provides Dom and sax methods to parse XML documents.
Read XML using the DOM method provided by org. W3C. Dom W3C
Org. xml. Sax provides core sax APIs
Org. xmlpull. V1

 

In addition, an XML class is also provided in the Android. util package. However, this class simply encapsulates the preceding package.

 

 

 

There are two main methods to read XML: Dom and SAX (Simple API for XML). Here we will describe these two methods respectively.

Dom (Document Object Model) defines a set of interfaces for parsing XML documents. The parser reads the entire document, constructs a tree structure with resident memory, and then the code can use

 

DOM interface to operate the entire tree structure of the group. The other points are as follows:

  • Advantage: the entire document tree is in the memory for easy operation. It supports deletion, modification, and re-arrangement among other functions.
  • Disadvantage: transferring the entire document to memory (often containing a large number of useless nodes) wastes time and space.
  • Usage: Once the file is parsed, you need to access the data multiple times and have sufficient resources (such as memory and CPU ).

 

To solve these problems caused by Dom parsing XML, the following occurs. The XML document for parsing Sax is event-driven. For details, read the XML (in)-Sax for reading and writing android. When the parser finds that the element starts, the element ends, and the text or document starts or ends, it sends events and writes code in the program to respond to these events. The features are as follows:

  • Advantage: the entire document does not need to be transferred in advance, which consumes less resources. Especially in Embedded environments, it is strongly recommended to use SAX to parse XML documents.
  • Disadvantage: Unlike Dom, documents are stored in the memory for a long time, and the data is not persistent. After an event, if data is not stored, the data will be lost.
  • Application scenarios: machine performance is limited, especially in Embedded environments such as Android. We strongly recommend using Sax to parse XML documents.

Most of the time, using Sax is safer, and Android provides a traditional method of using Sax and a convenient sax package. If the XML document is small, Dom may be a simple method. If the XML document is large but only a part of the document is required, the XML pull parser may be more effective. Finally, for the compilation of XML, the pull parser package also provides a convenient method. For details, read the XML (lower) for reading and writing data into XML documents in Android. Therefore, android can meet our requirements to a certain extent regardless of our XML requirements.

 

Next we will introduce in detail the idea of reading XML documents using Dom, which is basically the same as the XML structure. First load the XML document, then obtain the root node (element) of the document, and then obtain the list of all child nodes in the root node (nodelist ), then, obtain the nodes to be read from the subnode list. Based on the above ideas, a brief example of reading XML files is as follows:

 

Import javax. XML. parsers. documentbuilder; <br/> Import javax. XML. parsers. documentbuilderfactory; <br/> Import javax. XML. parsers. parserconfigurationexception; <br/> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. element; <br/> Import Org. w3C. dom. nodelist; <br/> Import Org. XML. sax. saxexception; <br/> Import android. content. context; <br/> void readxml () <br/>{< br/> documentbuilderfactory docbuilderfactory = NULL; <br/> documentbuilder docbuilder = NULL; <br/> document DOC = NULL; <br/> try {<br/> docbuilderfactory = documentbuilderfactory. newinstance (); <br/> docbuilder = docbuilderfactory. newdocumentbuilder (); <br/> // put the XML file in the Assets Directory <br/> Doc = docbuilder. parse (context. getresources (). getassets (). open ("weather. XML); <br/> // root element <br/> element root = Doc. getdocumentelement (); <br/> // do something here <br/> // get a nodelist by tagname <br/> nodelist = root. getelementsbytagname ("tag"); <br/> for (INT I = 0; I <nodelist. getlength (); I ++) <br/>{< br/> node nD = nodelist. item (I); <br/> // read node <br/>}< br/>} catch (ioexception e) {<br/>} catch (saxexception E) {<br/>} catch (parserconfigurationexception e) {<br/>} finally {<br/> Doc = NULL; <br/> docbuilder = NULL; <br/> docbuilderfactory = NULL; <br/>}< br/>} 

 

 


The above code is relatively simple and will not be parsed in detail here.

To use Dom to read XML files, you need to load the entire XML file. When the XML file is large, the memory of the Android device is insufficient. To avoid this problem, you can also use the sax method to read XML files. However, operations such as sorting nodes and adding nodes by sax are a little more complicated than Dom. Select an appropriate read Method Based on the XML file size and data processing requirements.

For XML writing, on the one hand, you can use the package described above; on the other hand, you can directly write data into files in the form of strings according to XML standards, this is also a good method. We recommend a good article in the IBM open-source community to use XML on Android. If you are interested, you can study it in depth.

Summary
XML, as a simple file format, serves as the basis for data exchange on the network and is supported by most databases (directly inserting XML files into the database). Reading and Writing XML files is only the basis, I believe it will be frequently used in future network and database development.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.