[java] how to create Schema object from DTD file?

Discussion in 'XML' started by Marcin Cenkier, Nov 17, 2005.

  1. Hi,

    I can create a schema from xsd file:

    Schema s =
    SchemaFactory.newInstance(_XMLConstants.W3C_XML_SCHEMA_NS_URI_).newSchema(new
    StreamSource(res.getInputStream()));

    but when using XMLConstants.XML_DTD_NS_URI then an exception is thrown:

    Schema s =
    SchemaFactory.newInstance(_XMLConstants.XML_DTD_NS_URI_).newSchema(new
    StreamSource(res.getInputStream()));

    java.lang.IllegalArgumentException: http://www.w3.org/TR/REC-xml
    at javax.xml.validation.SchemaFactory.newInstance(Unknown Source)

    I'm using xerces 2.7.1, do you know what's the matter?

    Regards,
    M

    --
    Marcin Cenkier [mailto_marcin.cenkier_at_cognifide_com]
     
    Marcin Cenkier, Nov 17, 2005
    #1
    1. Advertising

  2. Marcin Cenkier wrote:


    > but when using XMLConstants.XML_DTD_NS_URI then an exception is thrown:
    >
    > Schema s =
    > SchemaFactory.newInstance(_XMLConstants.XML_DTD_NS_URI_).newSchema(new
    > StreamSource(res.getInputStream()));
    >
    > java.lang.IllegalArgumentException: http://www.w3.org/TR/REC-xml
    > at javax.xml.validation.SchemaFactory.newInstance(Unknown Source)


    I don't think that is supported, see

    <http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html>
    whichs says:
    "To be compliant with the spec, the implementation is only required
    to support W3C XML Schema 1.0"
    and
    "Note that because the XML DTD is strongly tied to the parsing
    process and has a significant effect on the parsing process, it is
    impossible to define the DTD validation as a process independent from
    parsing. For this reason, this specification does not define the
    semantics for the XML DTD"


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 17, 2005
    #2
    1. Advertising

  3. Martin Honnen napisał(a):
    > I don't think that is supported, see
    >
    > <http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/SchemaFactory.html>
    >
    > whichs says:
    > "To be compliant with the spec, the implementation is only required to
    > support W3C XML Schema 1.0"
    > and
    > "Note that because the XML DTD is strongly tied to the parsing process
    > and has a significant effect on the parsing process, it is impossible to
    > define the DTD validation as a process independent from parsing. For
    > this reason, this specification does not define the semantics for the
    > XML DTD"


    Yeah, I've seen it, but still I'm pretty sure that there must be a way
    to validate xml using dtd?!? Maybe not by using Schema (which I thought
    would be implemented in xerces).
    I solved the problem by converting this dtd to xsd, but the question
    remains...

    Thanks,
    M


    --
    Marcin Cenkier [mailto_marcin.cenkier_at_cognifide_com]
     
    Marcin Cenkier, Nov 17, 2005
    #3
  4. Marcin Cenkier wrote:
    > but still I'm pretty sure that there must be a way
    > to validate xml using dtd?!?


    You can validate your XML code against a DTD while parsing.
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
     
    Johannes Koch, Nov 18, 2005
    #4
  5. Marcin Cenkier wrote:


    > Yeah, I've seen it, but still I'm pretty sure that there must be a way
    > to validate xml using dtd?!?


    Yes, there is one but not with the API you are trying to use which was
    added in Java 1.5 to allow schema validation.
    If you want to validate against a DTD then you can do that by parsing an
    XML document having a DOCTYPE declaration pointing to the DTD.
    You simply need to set up the parser so that it does validation while
    parsing, look into SAXParserFactory for instance where you can do
    parserFactory.setValidating(true)
    <http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/SAXParserFactory.html#setValidating(boolean)>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 18, 2005
    #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. Joseph Tilian
    Replies:
    0
    Views:
    355
    Joseph Tilian
    Dec 21, 2004
  2. Markus
    Replies:
    1
    Views:
    1,551
    Markus
    Nov 23, 2005
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,269
    Stanimir Stamenkov
    Apr 25, 2005
  4. test
    Replies:
    2
    Views:
    2,045
    Oliver Wong
    Jul 28, 2006
  5. pramodr
    Replies:
    3
    Views:
    842
    Peter Flynn
    Apr 5, 2009
Loading...

Share This Page