XML parsing in Android/Java

Source: Internet
Author: User

HTTP data organization

There are three data organization methods in HTTP network transmission.

1. html

2. xml

3. JSON

XML is called an extensible markup language. Like HTML, XML is a SGML (Standard Generic Markup Language). XML is

Cross-platform in the Internet environment, relying on content technology, is a powerful tool for processing structured document information. The extensible markup language XML is a simple data storage language, use a series of simple tags to describe data.

XML structure:

<? XML version = "1.0" encoding = "UTF-8">

<Persons>

<Person id = "23">

<Name> Jack </Name>

<Age> 30 </age>

</Person>

<Person id = "20">

<Name> rose </Name>

<Age> 25 </age>

</Person>

<Persons>

 

The XML structure is parsed as follows:

1. Nodes

2. Elements

3. attributes and attribute values

Due to the high scalability of XML, it needs stable basic rules to support expansion,

The syntax rule is:

1. Match the start and end tags

2. nested labels cannot be nested with each other

3. Case Sensitive

 

In Android, there are three ways to parse XML data:

1. Dom (Org. W3C. DOM)

The "Document Object Model" method produces a tree structure object after parsed XML

2. Sax (Org. xml. Sax)

Simpleapi for XML notifies the program in the form of events to parse the XML.

3. xmlpull (Org. xmlpull. V1)

Similar to the sax method, the program parses XML by pulling

 

Sax Technology

Sax is an event-driven xml api. The event stream defined by it can be specified from

The parser is passed to the XML structure of the special processing program code. In simple words, it is fast to parse.

Memory usage. This parser is suitable for Android and other mobile devices.

Advantages of using sax:

Because the advantage of Sax is stream processing, when a tag is encountered, it does not record the tag currently encountered.

That is to say, the information you know in the starteelement method is only the name and attribute of the current signature. As for the nested structure of the tag, the name of the Upper-layer tag, it is unknown whether any child element has information related to other structures.

 

 

 

Pull Technology

The pull parser runs in a similar way as the SAX Parser and provides similar events.

For example, you can use parser. Next () to start the next element and trigger the corresponding event.

The event is sent as code. Therefore, you can use a switch to select and process the event.

When parsing elements, call parser. nexttext () to obtain the next text element.

Pull features:

Simple structure: an interface, an exception, and a pull parser composed of a factory

Easy to use: the pull parser has only one important method: Next method, which is used to retrieve the next

Event, and it only has five common attributes

1. startdocument

2. start_tag

3. Text

4. end_tag

5. end_document

 

 

 

DOM technology

Dom is an XML document object model that can be used to directly access all parts of an XML document.

The document is simulated as a tree. Each component of the XML syntax represents a node. The Dom allows users to traverse the document tree and move from the parent node to the child node and brother node. And take advantage of the unique attributes of a node type (the element has attributes

, Text nodes have text data)

Node (each component in the XML document is a node)

Dom is as follows:
The entire document is a node document

Each XML tag is an element node.

The text contained in the XML element is a text node.

Each XML Attribute is a property node.

 

Comparison of sax, Dom, and pull

Sax and pull use less memory resolution than Dom, and are more suitable for Android mobile phone development.

 

 

Demo (three methods are available ):
Http://download.csdn.net/detail/shuipingzildy/5300746

 

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.