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