XML DOM - Properties and Methods
Properties and methods define the programming interface for 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 through a standard set of properties and methods.
propertyOften used in the way of "what something is" (for example the node name is "book").
MethodOften used in the way of "do something to 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 - the attribute nodes of x
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 into x
- x.removeChild(node) - delete a child node from x
Note: In the list above, x is a node object.
Example
from books.xml's
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue
After this statement is executed, the value stored in txt is "Everyday Italian".
Explanation:
- xmlDoc - the XML DOM object created by the parser
- getElementsByTagName("title")[0] - the first
element - childNodes[0] -
the element's first child node (text node) - nodeValue - the node's value (the text itself)