XML operations using JavaScript

Source: Internet
Author: User
Tags xml attribute
Javascript operations on XML are completed through xml dom.
So what is xml dom?
Xml dom is:

  • Standard Object Model for XML
  • Standard programming interface for XML
  • Neutral in platform and language
  • W3C standards

The xml dom defines the objects and attributes of all XML elements and the methods (interfaces) for accessing them ).
That is to say:
Xml dom is used to query, add, modify, and delete XML elements.

Before proceeding, let's take a look at the definition of XML.
See the following XML file (books. XML ):
<? XML version = "1.0" encoding = "UTF-8"?>
<Bookstore>
<Book category = "Cooking">
<Title lang = "en"> everyday Italian </title>
<Author> Giada De laurentiis </author>
<Year> 2005 </year>
<Price> 30.00 </price>
</Book>
<Book category = "children">
<Title lang = "en"> Harry Potter </title>
<Author> j k. Rowling </author>
<Year> 2005 </year>
<Price> 29.99 </price>
</Book>
<Book category = "Web">
<Title lang = "en"> XQuery kick start </title>
<Author> James McGovern </author>
<Author> Per bothner </author>
<Author> Kurt Cagle </author>
<Author> James Linn </author>
<Author> vaidyan#nagarajan </author>
<Year> 2003 </year>
<Price> 49.99 </price>
</Book>
<Book category = "Web">
<Title lang = "en"> learning XML </title>
<Author> Erik T. Ray </author>
<Year> 2003 </year>
<Price> 39.95 </price>
</Book>
</Bookstore>
In the preceding XML, the root node is <bookstore>. All other nodes in this document are included in <bookstore>.
The root node <bookstore> has four <book> nodes.
The first <book> node has four nodes: <title>, <author>, <year>, and <price>. Each node contains a text node, "Everyday Italian ", "Giada De laurentiis", "2005" and "30.00 ".

Each component in the XML document is a node.

Node

According to the Dom, each component in the XML document is a node.
Dom is as follows:

  • The entire document is a document Node
  • 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.
  • Annotation belongs to the annotation Node
Text is always stored in text nodes.

A common error in Dom processing is that an element node contains text.
However, the text of the element node is stored in the text node.
In this example: <year> 2005 </year>, an element node <year> has a text node with a value of "2005.
2005 "is not the value of the <year> element!

Xml dom treats xml dom documents as a node-tree ).
All nodes in the tree are related to each other.

Xml dom node tree

Xml dom treats XML documents as a tree structure. This tree structure is called a node tree.
You can access all nodes through this tree. They can be modified or deleted, or new elements can be created.
This node tree shows the set of nodes and their relationships. This tree starts from the root node and then grows branches to the text node at the lowest level of the tree:

Parent, child, and sibling nodes

Nodes in the node tree have hierarchical relationships with each other.
Parent, and sibling nodes are used to describe the relationship. A parent node has a child node. A child node at the same level is called a sibling node.

  • In the node tree, the top node becomes the root node.
  • Each node except the root node has a parent node.
  • Nodes can have any number of subnodes
  • Leaves are nodes without subnodes
  • A sibling node is a node with the same parent node.

In the preceding XML, the <title> element is the first child node of the <book> element, and the <price> element is the last child node of the <book> element.
In addition, the <book> element is the parent node of the <title>, <author>, <year>, and <price> elements.

Note: parent node, child node: Children node, same level node: sibling Node

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.