XML DOM - Properties and methods


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


Programming Interface

DOM models 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 by a standard set of properties and methods.

PropertyOften used in a way that describes "what something is" (for example, the node name is "book").

MethodsOften used in a way that describes "what to do with something" (for example, delete the "book" node).


XML DOM properties

Some typical DOM properties:

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

Note: In the list above, 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) - Remove a child node from x

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


Example

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

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

After this statement executes, the value stored in txt is "Everyday Italian".

Explanation:

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

other extensions