I checked the article, but didn't find answers. I have a simple example downloaded from ibm web-site which shows how to parse and print xml using DOM.
I didn't include the print method here.
*************************************************************
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.*;
/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree.
*/
public class domOne
{
public void parse(
String uri)
{
Document
doc = null;
try
{
DOMParser parser = new DOMParser();
parser.parse(uri);
doc = parser.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
}
}
**************************************************************
The following is a sample xml file and dtd file downloaded from IBM website with the above example.
**************************
<?xml version="1.0"?>
<!DOCTYPE sonnet SYSTEM "sonnet.dtd">
<sonnet type="Shakespearean">
<author>
<last-name>Shakespeare</last-name>
<first-name>William</first-name>
<nationality>British</nationality>
<year-of-birth>1564</year-of-birth>
<year-of-death>1616</year-of-death>
</author>
<title>Sonnet 130</title>
<lines>
<line>My mistress' eyes are nothing like the sun,</line>
<line>Coral is far more red than her lips red.</line>
<line>If snow be white, why then her breasts are dun,</line>
<line>If hairs be wires, black wires grow on her head.</line>
<line>I have seen roses damasked, red and white,</line>
<line>But no such roses see I in her cheeks.</line>
<line>And in some perfumes is there more delight</line>
<line>Than in the breath that from my mistress reeks.</line>
<line>I love to hear her speak, yet well I know</line>
<line>That music hath a far more pleasing sound.</line>
<line>I grant I never saw a goddess go,</line>
<line>My mistress when she walks, treads on the ground.</line>
<line>And yet, by Heaven, I think my love as rare</line>
<line>As any she belied with false compare.</line>
</lines>
</sonnet>
****************************************************
<!-- sonnet.dtd -->
<!-- sonnet is the root of the document -->
<!ELEMENT sonnet (author,title?,lines)>
<!-- the default sonnet type is "Shakespearean" -->
<!ATTLIST sonnet type (Shakespearean | Petrarchan)
"Shakespearean">
<!-- author contains information about the author -->
<!ELEMENT author (last-name,first-name,nationality,
year-of-birth?,year-of-death?)>
<!-- last-name, first-name, nationality, year-of-birth,
and year-of-death are all elements inside author. -->
<!ELEMENT last-name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT nationality (#PCDATA)>
<!ELEMENT year-of-birth (#PCDATA)>
<!ELEMENT year-of-death (#PCDATA)>
<!-- The title of the sonnet -->
<!ELEMENT title (#PCDATA)>
<!-- The lines element contains the 14 lines of the
sonnet. -->
<!ELEMENT lines (line,line,line,line,
line,line,line,line,
line,line,line,line,
line,line)>
<!ELEMENT line (#PCDATA)>
****************************************************
According to the dtd, I'd think that with no first name or last name in the xml file, the parser should throw an exception. But it didn't. This is really frustrating. I have been looking for answers.
Any help is greatly appreciated!!!
[This message has been edited by Caroline Iux (edited June 22, 2001).]