Latest web development tutorials

XML DOM Methods

Properties and methods of the XML DOM defines a programming interface.


Programming Interface

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

On DOM programming interface through a standard set of properties and methods defined.

Properties often follow "what something is" way to use (for example, the node name is "book").

Methods often follow "to do something" way to use (such as deleting the "book" node).


XML DOM property

Some typical DOM properties:

  • x.nodeName - x name
  • x.nodeValue - the value of x
  • x.parentNode - x's parent
  • Child nodes of x - x.childNodes
  • Attribute node x - x.attributes

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


XML DOM Methods

  • x.getElementsByTagName(name) - get all elements with the specified tag name
  • x.appendChild(node) - insert a child node to x
  • x.removeChild(node) - Removes the child node from x

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


Examples

Get text from books.xml the <title> element of JavaScript code:

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

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

Explanation:

  • xmlDoc - XML DOM objects created by the parser
  • getElementsByTagName ( "title") [0 ] - the first <title> element
  • childNodes [0] - <title> first child node (text node) elements
  • nodeValue - value (the text itself) node