I am attempting to parse an extremely
simple xml file that has an embedded DTD
using java sax2. Here is the xml file:
<?xml version="1.0" ?>
<!DOCTYPE foo [
<!ELEMENT foo (#PCDATA)>
]>
<foo>
<bar/>
</foo>
Notice I intentionally made an error by
not defining bar. OK. So now I write some
code to parse it as follows:
---
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class Simple
{
public static void main(String argv[])
{
String file = argv[0];
DefaultHandler handler = new MyHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
SAXParser reader = factory.newSAXParser();
reader.getXMLReader().setFeature("http://xml.org/sax/features/validation", true);
reader.parse(new File(file), handler);
}
catch (Exception e)
{
e.printStackTrace();
return;
}
}
}
---
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class MyHandler extends DefaultHandler
{
public void startDocument() throws SAXException
{
}
public void endDocument() throws SAXException
{
}
public void startElement
(
String uri,
String localName,
String elementName,
Attributes attributes
) throws SAXException
{
System.out.println("start " + elementName);
}
public void endElement
(
String uri,
String localName,
String elementName
) throws SAXException
{
System.out.println("end " + elementName);
}
public void characters
(
char[] ch,
int start,
int length
) throws SAXException
{
System.out.println("characters: " + new String(ch, start, length));
}
}
---
I build it and it parses the file without complaining. That make me think that
it is ignoring the DTD entirely. What am I doing wrong???
Dean Hoover