XML DOM - Properties and Methods
Properties and methods define the programming interface for XML DOM.
Programming Interface
The DOM represents XML as a set of node objects. These nodes can be accessed through JavaScript or other programming languages. In this tutorial, we use JavaScript.
The programming interface of the DOM is defined by a set of standard properties and methods.
Properties are often used in the way "what something is" (e.g., node name is "book").
Methods are often used in the way "what to do with something" (e.g., remove "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 above list, 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) - remove a child node from x
Note: In the above list, 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 is executed, txt holds the value "Everyday Italian".
Explanation:
xmlDoc - the XML DOM object created by the parser
getElementsByTagName("title")[0] - the first <title> element
childNodes[0] - the first child node of the <title> element (text node)
nodeValue - the value of the node (the text itself)