XML Tutorial: XMLDOM node list and specified node map

Source: Internet
Author: User
NodeName, nodeValue, and nodeType contain information about nodes. DOMNodeList and NamedNodeMap this section describes the concepts of NodeList and NamedNodeMap, and their differences. DOMNodeList when we apply something like childNodes or getElemen

NodeName, nodeValue, and nodeType contain information about nodes.

DOM Node List and NamedNodeMap

This section describes the concepts of NodeList and NamedNodeMap and their differences.

DOM Node List

When we apply attributes or methods like childNodes or getElementsByTagName (), we will receive a NodeList object.

The NodeList object represents an ordered node list.

You can use their index numbers (starting from 0) to visit these nodes in the node list.

Note:In a node list, nodes are returned in the order they are specified in XML.

See the following XML File: books. xml

Now, we need to use the getElementsByTagName ('title') method to create allThe node list of the element. The following figure shows the list of returned nodes: </P>

The following code snippet can be viewed from the firstGet text in the element: </P>

getElementsByTagName('title')[0].childNodes[0].nodeValue

Output:

Everyday Italian
Get the length of the node list

The node list can be updated. If an element is deleted or added, the list is automatically updated.

The node list has a useful attribute: length. The length attribute returns the number of nodes in a node list.

The following code snippet can be obtained in 'books. XML '.Number of elements: </P>

getElementsByTagName('title').length 

Output:

4

After you know the length of a node list, you can easily traverse the list cyclically and extract the value you need.

The following code snippets traverse allAnd output their values: </P>

//the x variable will hold a NodeListvar x=getElementsByTagName('title')for (i=0;i
 ')  }

Output:

Everyday ItalianHarry PotterXQuery Kick StartLearning XML
DOM NamedNodeMap

When attributes attribute is applied to an element, a NamedNodeMap object is received.

A NamedNodeMap represents an unordered list of attribute nodes.

You can use a node name to access a node in NamedNodeMap.

Note:In a NamedNodeMap, nodes are not returned in any special order.

Get the NamedNodeMap length

NamedNodeMap maintains its own updates. If an element is deleted or added, the list is also updated.

NamedNodeMap also has the length attribute. The length attribute returns the number of nodes in the list.

See the following XML File: books. xml

The following code snippet obtains the first one in 'books. XML '.Element attribute quantity: </P>

getElementsByTagName('title')[0].attributes.length 

Output:

1 
Obtain the target value of an item in NamedNodeMap.

The getNamedItem () method of the NamedNodeMap object can be used to retrieve a specified node.

The following code snippet shows how to output each Value of the 'category' attribute in the element:

xmlDoc=loadXMLDoc('books.xml');var x=xmlDoc.getElementsByTagName('book');for(i=0;i
 ')  } 

Output:

COOKINGCHILDRENWEBWEB

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.