Android read/write XML (on) -- Package Description

Source: Internet
Author: User
Document directory
  • Summary

Note: This article from the http://www.moandroid.com.

 

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 android
The package provided by the SDK for reading and writing XML.

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
Java programming language. Android SDK does not provide standard Java Runtime Environment (JRE)
Provides all available features, but it supports a large part of them. The Java platform supports using XML in many different ways, and most Java APIs related to XML
It is fully supported on Android. For example, Java's Simple API for XML (SAX) and document
Object Model (DOM) is available on Android. These APIs have been a part of Java Technology for many years and are relatively new.
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 has been confirmed to be available in
Android. The android SDK provides the following package to support XML read/write:

Package
Features
Javax. xml
Define Core XML constants and functions according to XML specifications.
Javax. xml. parsers
Provides Dom and sax methods to parse XML documents
Org. W3C. Dom
Read XML using the DOM method provided by W3C
Org. xml. Sax
Provides core sax APIs
Org. xmlpull. V1

In addition
Quantity
The data package also provides a class XML, but this class simply encapsulates the above 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 the DOM interface.
The entire tree structure of the operating 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 sax parsing XML document is event-driven. For details, please read the android read and write XML (in)-Sax "href =" http://www.moandroid.com /? P = 821 "target =" _ blank "> Android reads and writes XML (medium)-Sax
. When the parser finds that the element starts or ends, and the text or document starts or ends, it sends
Event, write code in the program to respond to these events. Its 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, as well as a convenient
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
The parser may be a more effective method. Finally, for the compilation of XML, pull parser package also provides a convenient way, please read the android read and write XML (below)-data write XML document "href =" http://www.moandroid.com /? P = 868 "target =" _ blank "> Android reads/writes XML (lower) -- writes data to XML documents
. Therefore, no matter what our XML requirements are, Android
It can satisfy our needs to a certain extent.

Next we will introduce in detail the idea of reading XML documents using Dom, which is basically the same as the XML structure. Load the XML document first, and then obtain
Take the root node (element) of the document, obtain the list of all child nodes (nodelist) in the root node, and then use it to obtain the nodes to be read from the child node list. According to the above ideas
Briefly write an example to read the XML file:
Import javax. xml. parsers. documentbuilder;
Import javax. xml. parsers. documentbuilderfactory;
Import javax. xml. parsers. parserconfigurationexception;
Import org. W3C. Dom. Document;
Import org. W3C. Dom. element;
Import org. W3C. Dom. nodelist;
Import org. xml. Sax. saxexception;
Import Android. content. context;
Void readxml ()
{
Documentbuilderfactory docbuilderfactory = NULL;
Documentbuilder docbuilder = NULL;
Document Doc = NULL;
Try {
Docbuilderfactory = documentbuilderfactory. newinstance ();
Docbuilder = docbuilderfactory. newdocumentbuilder ();
// Put the XML file in the Assets Directory
Doc =
Docbuilder. parse (context. getresources (). getassets (). Open ("weather. xml "));
// Root element
Element root = Doc. getdocumentelement ();
// Do something here
// Get a nodelist by tagname
Nodelist = root. getelementsbytagname ("tag ");
For (INT I = 0; I <nodelist. getlength (); I ++)
{
Node nD = nodelist. item (I );
// Read Node
}
} Catch (ioexception e ){
} Catch (saxexception e ){
} Catch (parserconfigurationexception e ){
} Finally {
Doc = NULL;
Docbuilder = NULL;
Docbuilderfactory = NULL;
}
}

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 XML file is read using the sax method, but operations on node sorting and adding nodes by the sax method are a little more complicated than Dom. Select an appropriate one based on the XML file size and data processing needs.
Read method.

For XML writing, you can use the package described earlier. In addition, you can directly write data to files in the form of strings according to XML standards.
Very good method. We recommend a good article in the IBM open-source community for Android.
XML
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). xml file read/write is only a basic
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.