sax ignoring DTD?

D

Dean A. Hoover

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));
}
}
 
M

Martin Honnen

Dean said:
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 think you need to define an error handler (implement the error method)
to have errors reported, otherwise they don't show up.

At least I get validation errors reported with SUN JDK 1.4 and the following

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXParseException;

public class SAXTest extends DefaultHandler {
protected static String url = "test20031007.xml";

public static void main (String[] args) {
SAXTest saxTest = new SAXTest();
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setValidating(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
String uriToParse;
if (args.length > 1) {
uriToParse = args[0];
}
else {
uriToParse = url;
}
saxParser.parse(uriToParse, saxTest);
}
catch (Exception e) {
System.out.println("Error occured: " + e);
}
}

public void error (SAXParseException parseError) {
System.out.println("Error during parsing: " + parseError);
}
}
 
D

Dean A. Hoover

Thanks. That works.

Martin said:
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 think you need to define an error handler (implement the error method)
to have errors reported, otherwise they don't show up.

At least I get validation errors reported with SUN JDK 1.4 and the
following

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXParseException;

public class SAXTest extends DefaultHandler {
protected static String url = "test20031007.xml";

public static void main (String[] args) {
SAXTest saxTest = new SAXTest();
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setValidating(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
String uriToParse;
if (args.length > 1) {
uriToParse = args[0];
}
else {
uriToParse = url;
}
saxParser.parse(uriToParse, saxTest);
}
catch (Exception e) {
System.out.println("Error occured: " + e);
}
}

public void error (SAXParseException parseError) {
System.out.println("Error during parsing: " + parseError);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top