[jdom-interest] [Urgent] Not able to validate XML file aginst XML schema using JDOM SAX Builder.

Navneet Agarwal navneetag at hotmail.com
Fri Jun 27 08:54:37 PDT 2003


Hi,
    I'm trying to validate an XML file against the XML schema but I'm always 
getting a JDOM exception. I'm always getting the same exception as below, 
even for the validated XML files, which i had downloaded from Internet and 
was able to successfully validate using XML editing tools like XML Spy. I'm 
not sure if the jdom.jar file i have is not the right one or is it to do 
with the xml file itself or is it the JDOM limitation. In my previous 
project I was able to validate an XML file against a DTD using the same JDOM 
SAX Builder. I've the latest jdom.jar file in my classpath. PLEASE HELP!!!

JDOM Exception:
---------------------
org.jdom.input.JDOMParseException: Error on line 2: Element type "countries" 
must be declared.
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:466)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:769)
        at demo.ValidDocument.build(ValidDocument.java:30)
        at demo.ValidDocument.main(ValidDocument.java:44)
Caused by: org.xml.sax.SAXParseException: Element type "countries" must be 
declared.
        at 
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1060)
        at 
org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1432)
        at 
org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:2930)
        at 
org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:924)
        at 
org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1858)
        at 
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1001)
        at 
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:952)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:455)
        ... 3 more
Caused by: org.xml.sax.SAXParseException: Element type "countries" must be 
declared.
        at 
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1060)
        at 
org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1432)
        at 
org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:2930)
        at 
org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:924)
        at 
org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1858)
        at 
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1001)
        at 
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:952)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:455)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:769)
        at demo.ValidDocument.build(ValidDocument.java:30)
        at demo.ValidDocument.main(ValidDocument.java:44)
Caused by: org.xml.sax.SAXParseException: Element type "countries" must be 
declared.
        at 
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1060)
        at 
org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1432)
        at 
org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:2930)
        at 
org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:924)
        at 
org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1858)
        at 
org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1001)
        at 
org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:952)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:455)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:769)
        at demo.ValidDocument.build(ValidDocument.java:30)
        at demo.ValidDocument.main(ValidDocument.java:44)


If I uncomment out the line:

builder.setProperty("http://apache.org/xml/prope
rties/schema/external-noNamespaceSchemaLocation", "countries.xsd");

I get the following JDOM Exception:
----------------------------------------------
org.jdom.JDOMException: 
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation 
property not recognized for SAX driver org.apache.xerces.parsers.SAXParser
        at 
org.jdom.input.SAXBuilder.internalSetProperty(SAXBuilder.java:750)
        at 
org.jdom.input.SAXBuilder.setFeaturesAndProperties(SAXBuilder.java:677)
        at org.jdom.input.SAXBuilder.createParser(SAXBuilder.java:527)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:426)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:769)
        at demo.ValidDocument.build(ValidDocument.java:30)
        at demo.ValidDocument.main(ValidDocument.java:44)


Java Code: ValidDocument.java
-------------------------------------------

package demo;

import java.io.*;
import org.jdom.JDOMException;
import java.util.List;
import java.util.Iterator;
import java.lang.String;
import org.jdom.input.SAXBuilder;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;


public class ValidDocument {
    // XML file to read
    File file;

    public ValidDocument(File file) {
        this.file = file;
    }

    public Document build()
        throws JDOMException, IOException {

        // Create new SAXBuilder, using default parser
        SAXBuilder builder = new 
SAXBuilder("org.apache.xerces.parsers.SAXParser", true);

         
builder.setFeature("http://apache.org/xml/features/validation/schema", 
true);
         
builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 
"countries.xsd");
         Document doc = builder.build(new FileInputStream(file));

         return doc;
    }

    public Document validate(Document doc)
        throws JDOMException, IOException {
        return doc;
    }

    public static void main(String[] args) {
        try {
            File file = new File(args[0]);
            ValidDocument validDocument = new ValidDocument(file);
            Document doc = validDocument.build();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        System.exit(0);
    }
}


XML file: countries.xml
--------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<countries xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="countries.xsd">
	<country name="England" capital="London"/>
	<country name="Wales" capital="Cardiff"/>
	<country name="Scotland" capital="Edinburgh"/>
</countries>


XML Schema: countries.xsd
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="countries">
		<xs:complexType>
			<xs:sequence minOccurs="0" maxOccurs="unbounded">
				<xs:element ref="country"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="country">
		<xs:complexType>
			<xs:attribute name="name" type="xs:string" use="required"/>
			<xs:attribute name="capital" type="xs:string" use="required"/>
		</xs:complexType>
	</xs:element>
</xs:schema>

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail




More information about the jdom-interest mailing list