DOM Tutorial 1
From Progzoo
These questions use the following XML document.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE stock [
<!ELEMENT stock (item*)>
<!ELEMENT item EMPTY>
<!ATTLIST item id ID #REQUIRED
legend CDATA #REQUIRED
price CDATA #REQUIRED>
]>
<stock>
<item id="E1" price="50" legend="Pr-Burger" />
<item id="E5" price="15" legend="Crisp S+V" />
<item id="E6" price="15" legend="Crisp C+O" />
<item id="E7" price="50" legend="Flat Cola" />
</stock>
Notice:
- The BarCode attribute has been specified as ID
- The root node is
stock
You will need the following methods:
getElementById(id) getNodeName() getParentNode() getNodeType()
http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/java-language-binding.html lists the properties and methods that should be available to you.
DOM Introduction
The getDocumentElement() method returns the single top level node of the document. We can access the methods of the Element object and the methods inherited from the Node object.
- Change the program so that is shows the Node Type instead of the Node Name of the document element.
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
getElementById
The method getElementById(t) is a handy method of Document.
It returns an Element
- Change the program so that it prints the legend for item E7
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]
getParentNode
The method getParentNode() may be used to navigate up the document.
- Change the program so that it prints node name for the parent of element e5
[Font]
[Default]
[Show]
[Resize]
[History]
[Profile]