why is this not validating?

Discussion in 'XML' started by scorpion, Apr 29, 2004.

  1. scorpion

    scorpion Guest

    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.createSAXParseException(Unknown
    Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at
    org.apache.xerces.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown
    Source)
    at
    org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown
    Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
    Source)
    at
    org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown
    Source)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
    Source)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.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(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at test.resolver.TestIdResolver.main(TestIdResolver.java:36)
    org.xml.sax.SAXParseException: Document is invalid: no grammar found.
    at
    org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
    Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at
    org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown
    Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
    Source)
    at
    org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown
    Source)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
    Source)
    at
    org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.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(Unknown Source)
    at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    at test.resolver.TestIdResolver.main(TestIdResolver.java:36)

    What's wrong with that code?

    TIA
     
    scorpion, Apr 29, 2004
    #1
    1. Advertising

  2. scorpion

    scorpion Guest

    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.createSAXParseException(Unknown
    > Source)
    > at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    > at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    > at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    > at
    > org.apache.xerces.impl.dtd.XMLDTDValidator.rootElementSpecified(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown
    > Source)
    > at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.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(Unknown Source)
    > at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    > at test.resolver.TestIdResolver.main(TestIdResolver.java:36)
    > org.xml.sax.SAXParseException: Document is invalid: no grammar found.
    > at
    > org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
    > Source)
    > at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    > at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    > at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    > at
    > org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown
    > Source)
    > at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentScannerImpl$ContentDispatcher.scanRootElementHook(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
    > Source)
    > at
    > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.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(Unknown Source)
    > at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
    > at test.resolver.TestIdResolver.main(TestIdResolver.java:36)
    >
    > What's wrong with that code?
    >
    > TIA
     
    scorpion, Apr 29, 2004
    #2
    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. JV
    Replies:
    4
    Views:
    2,985
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    470
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    921
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,073
    Smokey Grindel
    Dec 2, 2006
  5. Skybuck Flying
    Replies:
    16
    Views:
    689
    tragomaskhalos
    Aug 25, 2007
Loading...

Share This Page