XML DOM tutoria...login
XML DOM tutorial
author:php.cn  update time:2022-04-13 15:27:56

DOM methods


XML DOM - Properties and Methods


Properties and methods define the programming interface to the XML DOM.


Programming interface

DOM simulates XML as a series of node objects. Nodes can be accessed through JavaScript or other programming languages. In this tutorial, we use JavaScript.

The programming interface to the DOM is defined through a set of standard properties and methods.

Attributes are often used in terms of "what something is" (for example, the node name is "book").

Methods are often used in the form of "do something to something" (such as deleting the "book" node).


XML DOM attributes

Some typical DOM attributes:

  • x.nodeName - the name of x

  • x.nodeValue - The value of x

  • x.parentNode - The parent node of x

  • x.childNodes - The x's Child node

  • x.attributes - Attribute node of x

Note: In the above list, x is a node object.


XML DOM methods

  • x.getElementsByTagName(name) - Gets all elements with the specified tag name

  • x.appendChild(node) - Insert a child node into x

  • x.removeChild(node) - Delete child nodes from x

#Note: In the above list, x is a node object.


Example

JavaScript code to get text from the <title> element in books.xml:

txt=xmlDoc.getElementsByTagName("title")[ 0].childNodes[0].nodeValue

After this statement is executed, the value saved in txt is "Everyday Italian".

Explanation:

  • xmlDoc - XML ​​DOM object created by the parser

  • getElementsByTagName("title")[0] - the first <title> element

  • ##childNodes[0] - the <title> element The first child node (the text node)

  • nodeValue - the value of the node (the text itself)