Document Object Model (DOM) Level 1 specification describes the memory layout of an XML document with tree representation. Each document is encapsulated into Document object and root element. Example uses the following DTD description for student records. We assume that the extension of an xml file is .sdb and for this type of XML document, example builds a JTable representation of class list.
<!ELEMENT street (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT aphone (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT surname (#PCDATA)> <!ELEMENT phones (aphone*)> <!ELEMENT address (street,city,state?,country)> <!ELEMENT student (name,surname,address,phones?)> <!ATTLIST student stdid ID #REQUIRED> <!ELEMENT students student+>
One record class list looks like the following.
<?xml version='1.0'> <students> <student stdid="A1231999"> <name>Bugs</name> <surname>Bunny</surname> <address> <street>Walnut</street> <city>Carrotville</city> <country>Cartoon Land</country> </address> <phones> <aphone>1CC-615-AD123</aphone> </phones> </student> </students>
This example contains the following files:
StudentTable object accepts the Document object and produces JTable representation from the class list. Let's look at the extractRecords() method:
void extractRecords(Document myDoc) { // Obtains the Document Root element Element dbRoot = myDoc.getDocumentElement(); NodeList bList = null; // Gets the set of "student" elements. if( dbRoot!=null && dbRoot.getTagName().equals("students") ) bList = dbRoot.getElementsByTagName("student"); if( bList!=null && bList.getLength()>0) { int i=0; // For each "student" element node, defines a StudentRecord object // and adds this object to the table. while( i<bList.getLength() ) { Node current = bList.item(i++); if( current!=null && current.getNodeType()==org.w3c.dom.Node.ELEMENT_NODE && ((Element)current).getTagName().equals("student")) { StudentRecord sRec = new StudentRecord((Element)current); addStudentRecord(sRec); } }// end of while }// end of if }// end of extractRecords()
First we obtain the root element by using getDocumentElement() method, and then we get the set of student elements in this root node by using getElementsByTagName(). For each element in the set (bList), a StudentRecord object is defined and added to the table. In StudentRecord object, extract() method extracts the value of element and attributes from the Element node as follows;
void extract(Element ele) { if( ele.getTagName().equals("student") ) { org.w3c.dom.NamedNodeMap attribs = ele.getAttributes(); org.w3c.dom.Attr a1 = (org.w3c.dom.Attr) attribs.getNamedItem("stdid"); ID = a1.getValue(); NodeList bList = ele.getElementsByTagName("name"); if( bList!=null && bList.getLength()>0 ) extractName((Element)bList.item(0)); bList = ele.getElementsByTagName("surname"); if( bList!=null && bList.getLength()>0 ) extractSurname((Element)bList.item(0)); }// end of if }// end of extract()
Tag name of an element node can be obtained by getTagName() method. After it checks the tag name, it gets the list of attributes via getAttributes() method. stdid attribute value is selected from this set. student element node contains other sub element nodes such as name and surname elements, therefore, those elements are searched and the first node which has searched tag is selected. Then extractName() will extract the information from the name element node as;
/** extracts <b>Bunny</b> from <name>Bunny</name> */ void extractName(Element ele) { if( ele.getTagName().equals("name") ) { Node aNode = ele.getFirstChild(); if( aNode.getNodeType() == Node.TEXT_NODE ) name = ((Text)aNode).getData(); } }// end of extractName()
Again first, we check the tag name of the Element node, then it gets the first child node (getFirstChild()). This node should be a Text node so that we can extract the character sequence from this node through getData() method of Text node. When you start the application, you will see the following frame on your screen. When you click on the CIS folder, a HTML page describing the generals of the document is displayed. With double click on the CIS folder, you will get the second screen.
![]() |
Figure 1: A snapshot of GUI after user clicks on CIS folder. |
When user double clicks on the CIS folder, then interface displays the sub elements on the upper frame while the lower frame is used to display the action. When user clicks on the CIS720.html, it displays this HTML page. When user clicks on the Student.xml, it displays the XML document. When user clicks on Stiudent.sdb", it displays the Swing JTable. The following screen displays the class list from CIS720.html page.
![]() |
Figure 2: A snapshot of GUI after user clicks on CIS720.html. It uses the lower frame to display the CIS720.html page. |
The following screen displays the class list in XML format before any kind of presentation oriented processing is applied on the XML document.
![]() |
Figure 3: A snapshot of GUI after user clicks on Student.xml. |
The following screen displays the JTable representation of the XML document.
| ||
|