how to use jdk 1.5 & dtd to validate xml file

Discussion in 'Java' started by John_Woo, Jan 23, 2006.

  1. John_Woo

    John_Woo Guest

    Hi,

    I'm wondering, JDK5 brights with xml parser, validator, etc, but how
    can I use its package (or other like web-service packages) to validate
    a xml file by a dtd file, before let SAX proceed?

    I know an example in SUN's web
    (http://java.sun.com/developer/technicalArticles/xml/validationxpath/)

    , but it's using xml schema instead of dtd to validate.

    Any help?

    --
    Thanks lots in advance
    John
    Toronto
    John_Woo, Jan 23, 2006
    #1
    1. Advertising

  2. John_Woo

    zero Guest

    "John_Woo" <> wrote in news:1138029717.491824.252800
    @g44g2000cwa.googlegroups.com:

    > Hi,
    >
    > I'm wondering, JDK5 brights with xml parser, validator, etc, but how
    > can I use its package (or other like web-service packages) to validate
    > a xml file by a dtd file, before let SAX proceed?
    >
    > I know an example in SUN's web
    > (http://java.sun.com/developer/technicalArticles/xml/validationxpath/)
    >
    > , but it's using xml schema instead of dtd to validate.
    >
    > Any help?
    >


    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    SAXParser saxParser = factory.newSAXParser();
    saxParser.parse("myfile.xml", new MyDefaultHandler());

    where MyDefaultHandler extends DefaultHandler. This code will
    automatically validate the xml file if a DTD declaration is present.

    packages used are not in javax.xml but org.xml.sax.
    zero, Jan 23, 2006
    #2
    1. Advertising

  3. John_Woo

    John_Woo Guest

    Thanks for the info, but when I tried

    t.dtd
    <!ELEMENT c (trans*) >
    <!ATTLIST c >
    <!ELEMENT trans (EMPTY)>
    <!ATTLIST trans type CDATA #FIXED "7" >

    t.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE c SYSTEM "t.dtd">
    <c>
    <trans type="9" />
    </c>

    it's obviously that value 9 didn't match value 7 defined in dtd,
    but running the code below

    javax.xml.parsers.SAXParserFactory factory =
    javax.xml.parsers.SAXParserFactory.newInstance();

    factory.setValidating(true);
    javax.xml.parsers.SAXParser saxParser = factory.newSAXParser();
    saxParser.parse("t.xml", new My...());


    there was no exception found.

    Can u test it?

    John
    John_Woo, Jan 23, 2006
    #3
  4. John_Woo

    zero Guest

    "John_Woo" <> wrote in news:1138051619.256468.8820
    @f14g2000cwb.googlegroups.com:

    > Thanks for the info, but when I tried
    >
    > t.dtd
    > <!ELEMENT c (trans*) >
    > <!ATTLIST c >
    > <!ELEMENT trans (EMPTY)>
    > <!ATTLIST trans type CDATA #FIXED "7" >
    >
    > t.xml
    > <?xml version="1.0" encoding="UTF-8"?>
    > <!DOCTYPE c SYSTEM "t.dtd">
    > <c>
    > <trans type="9" />
    > </c>
    >
    > it's obviously that value 9 didn't match value 7 defined in dtd,
    > but running the code below
    >
    > javax.xml.parsers.SAXParserFactory factory =
    > javax.xml.parsers.SAXParserFactory.newInstance();
    >
    > factory.setValidating(true);
    > javax.xml.parsers.SAXParser saxParser = factory.newSAXParser();
    > saxParser.parse("t.xml", new My...());
    >
    >
    > there was no exception found.
    >
    > Can u test it?
    >
    > John
    >
    >


    Validation errors are nonfatal errors, and are ignored by the
    ErrorHandler implementation in DefaultHandler. To throw an exception
    when a validation error occurs, you need to override the error
    (SAXParseException) and/or warning(SAXParseException) methods.

    public void error(SAXParseException e)
    throws SAXParseException
    {
    throw e;
    }

    public void warning(SAXParseException e)
    throws SAXParseException
    {
    throw e;
    }

    See the JAXP tutorial at
    http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/
    zero, Jan 24, 2006
    #4
  5. John_Woo

    John_Woo Guest

    got it, Thanks lots.
    John_Woo, Jan 24, 2006
    #5
    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. Duane Morin
    Replies:
    1
    Views:
    829
    Patrick TJ McPhee
    Dec 11, 2003
  2. yw
    Replies:
    2
    Views:
    476
  3. Georg J. Stach
    Replies:
    7
    Views:
    7,897
    JAPISoft
    Sep 26, 2005
  4. test
    Replies:
    2
    Views:
    1,976
    Oliver Wong
    Jul 28, 2006
  5. Dundonald
    Replies:
    8
    Views:
    1,341
    Mike Schilling
    Oct 5, 2007
Loading...

Share This Page