Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM method


May 27, 2021 XML DOM


Table of contents


XML DOM - Properties and methods

This section introduces you to the properties and methods of XML DOM, please refer to the details of this section!

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


Programming interface

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

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

Properties are often used in the "what is something" way (for example, the node name is "book").

Methods are often used in the "what to do with something" approach (for example, removing the "book" node).


XML DOM property

Some typical DOM properties:

  • The name of x.nodeName - x
  • The value of x.nodeValue - x
  • The parent node of x.parentNode - x
  • x.childNodes - a child node of x
  • The property node of x.attributes - x

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


XML DOM method

  • x.getElementsByTagName ( name ) - Get all the elements with the specified label name
  • x.appendChild ( node ) - inserts child nodes into x
  • x.removeChild (node) - Remove child nodes from x

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


Get the JavaScript .xml the text from the element of the "books.xml .

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

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

Explain:

  • xmlDoc - XML DOM object created by the parser
  • getElementsByTagName ("title") - the first element
  • ChildNodes - the first child node of the element (text node)
  • nodeValue - the value of the node (text itself)