XML summary and xml learning Summary

Source: Internet
Author: User

XML summary and xml learning Summary

1. Basic XML concepts

Extensible Markup Language (XML) is used to mark electronic files so that they have a structured Markup Language. It can be used to mark data and define data types, is a source language that allows you to define your own markup language. The unified format, cross-platform and language have long been recognized in the industry.

* Top-down tree structure of xml data (documents)

* <Root>... </root> (a complete node), the content of the node in the middle of the name (title) of the root node (...)

* Each xml document has only one root node (the oldest node in the tree structure)

* Xml documents (data) are composed of multiple nodes (root node> subnode ....)

2. Comparison with JSON

JSON (JavaScript Object Notation) is a lightweight data exchange format with good readability and ease of writing. Data exchange can be performed between different platforms.

A. the XML format is uniform, compliant with the standard, and highly readable. The JSON format is simple, generally compressed, and less readable.

B. the XML file is large and occupies a large amount of bandwidth. JSON is compressed and has less bandwidth;

C. XML parsing requires a lot of resources and time; JSON is easy to parse.

 

Currently, most applications use JSON to transmit a large amount of data.

3. XML Parsing

There are two ways to parse XML: DOM and SAX:

  • When DOM parses XML, it reads the entire XML document and constructs a tree (node tree) with resident memory. By traversing the tree structure, it can retrieve any XML node and read its attributes and values. In addition, you can use XPath to directly query XML nodes.
  • SAX parses XML, which is based on the Event Notification mode. It reads and processes XML documents without waiting for the entire document to be loaded, when an object to be processed is encountered during the read and resolution process, a notification is sent to process the object.

--------------------------------------

XPath is the XML Path language. It is a language used to determine a part of the xml document. The XML-based tree structure of XPath provides the ability to search nodes in the data structure tree.

Node)

There are seven types of nodes in XPath: elements, attributes, text, namespaces, processing commands, comments, and document (Root) nodes. XML documents are treated as node trees. The root of a tree is called a document node or a root node.

See the following XML document:

 

<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<Bookstore>

<Book>

<Title lang = ""> ios development guide r </title>

<Author> Beijing </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

<Book>

<Title lang = ""> ios development guide r </title>

<Titlelang> Chinese </titlelang>

<Author> Beijing </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

</Bookstore>

<Otherroot> ...... There must be no second root node. </otherroot>

 

Node example in the XML document above:

 

<Bookstore> (document node, also called root node)

<Author> Beijing </author> (element node)

Lang = "en" (attribute node)

 

Basic value (or Atomic value)

 

The basic value is a node with no parent or child.

 

Example of the basic value:

Beijing

"Chinese"

 

Item)

 

A project is a basic value or node.

 

Node relationship

Parent)

Each element and attribute has a parent.

In the following example, the book element is the parent of the title, author, year, and price elements:

<Book>

<Title lang = ""> ios development guide r </title>

<Author> Beijing </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

Child)

Element nodes can have zero, one, or more sub-nodes.

In the following example, the title, author, year, and price elements are child of the book element:

<Book>

<Title lang = ""> ios development guide r </title>

<Author> Beijing </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

Siblings)

Nodes with the same parent

In the following example, the title, author, year, and price elements are all siblings:

<Book>

<Title lang = ""> ios development guide r </title>

<Author> Beijing </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

Advanced (Ancestor)

The parent and parent of a node.

In the following example, the first generation of the title element is the book element and the bookstore element:

<Bookstore>

<Book>

<Title lang = ""> ios development guide r </title>

<Author> Beijing </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

</Bookstore>

Descendant (Descendant)

The child of a node, the child of the child, and so on.

In the following example, the future generations of bookstore are book, title, author, year, and price:

<Bookstore>

<Book>

<Title lang = ""> ios development guide r </title>

<Author> qianfeng 3G </author>

<Year> 2012 </year>

<Price> 88.88 </price>

</Book>

</Bookstore>

 

*/

 

// XPath syntax

 

/*

XML instance document

We will use this XML document in the following example.

<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<Bookstore>

<Book>

<Title lang = "eng"> Harry Potter </title>

<Price> 29.99 </price>

</Book>

 

<Book>

<Title lang = "eng"> Learning XML </title>

<Price> 39.95 </price>

</Book>

<Title lang = "eng"> Learning XML </title>

</Bookstore>

 

Select Node

XPath uses path expressions to select nodes in XML documents. Nodes are selected by following the path or step.

The most useful path expressions are listed below:

Expression description

Nodename Selects all the subnodes of this node.

/Select from the root node.

// Select the nodes in the document from the current node that matches the selected node, regardless of their location.

. Select the current node.

.. Select the parent node of the current node.

@ Select attributes.

 

Instance

In the following table, we have listed some path expressions and expression results:

Path expression result

 

Bookstore Selects all the subnodes of the bookstore element.

 

/Bookstore select the root element bookstore.

Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element!

 

Bookstore/book Selects all the book elements that belong to the sub-elements of bookstore.

 

// Book Selects all the child elements of the book, regardless of their location in the document.

 

Bookstore // book Selects all the book elements belonging to the descendant of the bookstore element, regardless of where they are located under the bookstore.

 

// @ Lang select all attributes named lang.

 

Predicates)

It is used to find a specific node or a node that contains a specified value.

The predicates are embedded in square brackets.

 

Instance

In the following table, we list some path expressions with predicates and the results of the expressions:

Path expression result

/Bookstore/book [1] select the first book element that belongs to the bookstore sub-element.

 

/Bookstore/book [last ()] select the last book element that belongs to the bookstore sub-element.

 

/Bookstore/book [last ()-1] select the last and second book elements belonging to the bookstore sub-elements.

 

/Bookstore/book [position () <3] select the first two bookstore sub-elements.

 

// Title [@ lang] Selects all the title elements with the attribute lang.

 

// Title [@ lang = 'eng'] Selects all title elements and these elements have the lang attribute with the value of eng.

 

/Bookstore/book [price> 35.00] Selects all the book elements of the bookstore element, and the value of the price element must be greater than 35.00.

 

/Bookstore/book [price> 35.00]/title Selects all the title elements of the book element in the bookstore element, and the value of the price element must be greater than 35.00.

 

Select unknown Node

The XPath wildcard can be used to select unknown XML elements.

Wildcard description

* Match any element node.

@ * Match any attribute node.

Node () matches any type of node.

 

Instance

In the following table, we list some path expressions and the results of these expressions:

Path expression result

 

/Bookstore/* select all the child elements of the bookstore element.

 

// * Select all elements in the document.

 

// Title [@ *] Selects all the title elements with attributes.

// Attributes in the node cannot have duplicate names

Select several paths

You can select several paths by using the "|" operator in the path expression.

Instance

In the following table, we list some path expressions and the results of these expressions:

Path expression result

 

// Book/title | // book/price select all the title and price elements of the book element.

 

// Title | // price select all the title and price elements in the document.

 

/Bookstore/book/title | // price Selects all the title elements of the book element of the bookstore element and all the price elements in the document.

*/

-------------------------------------

 

 

-XML Parsing Library

-- IOS comes with two types:

The NSXMLParser-Sax method is easy to parse and needs to implement its proxy protocol method.

Libxml2 underlying library, capable of reading and parsing, based on c, also supports DOM, SAX, high resolution efficiency.

-- Common third-party libraries:

KissXML, GDataXML

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.