sax ignoring DTD?

Discussion in 'XML' started by Dean A. Hoover, Dec 7, 2003.

  1. 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
    Dean A. Hoover, Dec 7, 2003
    #1
    1. Advertising

  2. Dean A. Hoover wrote:

    > 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???


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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 7, 2003
    #2
    1. Advertising

  3. Thanks. That works.

    Martin Honnen wrote:
    >
    >
    > Dean A. Hoover wrote:
    >
    >> 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???

    >
    >
    > 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);
    > }
    > }
    >
    Dean A. Hoover, Dec 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Tilian
    Replies:
    0
    Views:
    340
    Joseph Tilian
    Dec 21, 2004
  2. Ronald Fischer
    Replies:
    4
    Views:
    1,746
    Ronald Fischer
    Mar 17, 2005
  3. test
    Replies:
    2
    Views:
    1,999
    Oliver Wong
    Jul 28, 2006
  4. Replies:
    1
    Views:
    1,273
    Joe Kesselman
    Aug 23, 2006
  5. marek jedlinski

    Can xml.sax NOT process the DTD?

    marek jedlinski, Jan 28, 2008, in forum: Python
    Replies:
    1
    Views:
    395
Loading...

Share This Page