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

M

Marcin Cenkier

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
 
M

Martin Honnen

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"
 
M

Marcin Cenkier

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
 
J

Johannes Koch

Marcin said:
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.
 
M

Martin Honnen

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.../SAXParserFactory.html#setValidating(boolean)>
 

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,774
Messages
2,569,596
Members
45,138
Latest member
NevilleLam
Top