DOM Tutorial 1

From Progzoo
Jump to navigation Jump to search

These questions use the following XML document.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE stock [
<!ELEMENT stock (item*)>
               legend CDATA #REQUIRED
               price CDATA #REQUIRED>
  <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" />


  • The BarCode attribute has been specified as ID
  • The root node is stock

You will need the following methods:

getElementById(id)               getNodeName()
getParentNode()                  getNodeType() 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]


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]


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]