By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

why is this not validating?

P: n/a
I have this problem that an xml instance is validated correctly
by xml tools, but not with my simple code, by setting the
validating flag to true.

--------------- Schema -----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xml:lang="EN"
targetNamespace="http://www.mydomain.com/demo/Test"
xmlns:test="http://www.mydomain.com/demo/Test"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="TestO" type="test:TestType"/>
<xs:complexType name="TestType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Value" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:schema>

---------------- Instance ----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<test:TestO xmlns:test="http://www.mydomain.com/demo/Test"
xsi:schemaLocation="http://www.mydomain.com/demo/Test
file:///home/csp/workspace/test-java/test.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="example-ncname">
<test:Name>string</test:Name>
<test:Value>1</test:Value>
</test:TestO>

This is validating correctly using XMLBuddy, XMLSpy, etc. But when
I try to do with the following code

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new SimpleErrorHandler());

Document document = db.parse(new
File("/home/csp/workspace/test-java/test.xml"));

It's reporting the errors

org.xml.sax.SAXParseException: Document root element "test:TestO", must
match DOCTYPE root "null".
at
org.apache.xerces.util.ErrorHandlerWrapper.createS AXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(U nknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.rootEle mentSpecified(Unknown
Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.handleS tartElement(Unknown
Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startEl ement(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanStartElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentScannerImpl$Cont entDispatcher.scanRootElementHook(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl$FragmentContentDispatcher.dispatch(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(U nknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at test.resolver.TestIdResolver.main(TestIdResolver.j ava:36)
org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at
org.apache.xerces.util.ErrorHandlerWrapper.createS AXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(U nknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.handleS tartElement(Unknown
Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startEl ement(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanStartElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentScannerImpl$Cont entDispatcher.scanRootElementHook(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl$FragmentContentDispatcher.dispatch(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(U nknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at test.resolver.TestIdResolver.main(TestIdResolver.j ava:36)

What's wrong with that code?

TIA
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Forget about this. Had to use "dbf.setAttribute()" instead.

scorpion wrote:
I have this problem that an xml instance is validated correctly
by xml tools, but not with my simple code, by setting the
validating flag to true.

--------------- Schema -----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xml:lang="EN"
targetNamespace="http://www.mydomain.com/demo/Test"
xmlns:test="http://www.mydomain.com/demo/Test"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="TestO" type="test:TestType"/>
<xs:complexType name="TestType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Value" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
</xs:complexType>
</xs:schema>

---------------- Instance ----------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<test:TestO xmlns:test="http://www.mydomain.com/demo/Test"
xsi:schemaLocation="http://www.mydomain.com/demo/Test
file:///home/csp/workspace/test-java/test.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="example-ncname">
<test:Name>string</test:Name>
<test:Value>1</test:Value>
</test:TestO>

This is validating correctly using XMLBuddy, XMLSpy, etc. But when
I try to do with the following code

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(new SimpleErrorHandler());

Document document = db.parse(new
File("/home/csp/workspace/test-java/test.xml"));

It's reporting the errors

org.xml.sax.SAXParseException: Document root element "test:TestO", must
match DOCTYPE root "null".
at
org.apache.xerces.util.ErrorHandlerWrapper.createS AXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(U nknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.rootEle mentSpecified(Unknown
Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.handleS tartElement(Unknown
Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startEl ement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanStartElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentScannerImpl$Cont entDispatcher.scanRootElementHook(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl$FragmentContentDispatcher.dispatch(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(U nknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at test.resolver.TestIdResolver.main(TestIdResolver.j ava:36)
org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at
org.apache.xerces.util.ErrorHandlerWrapper.createS AXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(U nknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportErro r(Unknown Source)
at
org.apache.xerces.impl.dtd.XMLDTDValidator.handleS tartElement(Unknown
Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startEl ement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanStartElement(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentScannerImpl$Cont entDispatcher.scanRootElementHook(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl$FragmentContentDispatcher.dispatch(Unknown
Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerI mpl.scanDocument(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse (Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(U nknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at test.resolver.TestIdResolver.main(TestIdResolver.j ava:36)

What's wrong with that code?

TIA

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.