JDOM: Problem on validating an xml file against a schema
843834Nov 26 2003 — edited Dec 9 2003Hi, JDOM practitioners,
Below is the sample code I used to validate an XML file against its schema.
import org.jdom.output.XMLOutputter;
import org.jdom.input.SAXBuilder;
import org.jdom.input.DOMBuilder;
import org.jdom.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Test
{
public static void main(String[] args)
{
try
{
SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser",true);
saxBuilder.setFeature(
"http://apache.org/xml/features/validation/schema", true);
saxBuilder.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation",
"file:///E:JDOM_Tutorial/Examples/test.xsd" );
Document doc = saxBuilder.build(new File(args[0]));
System.out.println( "Ddocument is valid");
}
catch(JDOMException jde)
{
printJdomException(jde);
}
catch(Exception e)
{
System.out.println( e.toString() );
}
}
private static void printJdomException(JDOMException jde)
{
System.out.println( "\n************JDOM Exception *********" );
System.out.println( "toString: " + jde.toString() );
System.out.println( "getMessage(): " + jde.getMessage() );
System.out.println( "getCause() " + jde.getCause() );
System.out.println( "\n************************************" );
}
}
I got the following exception.
E:\JDOM_Tutorial\Examples>javac Test.java
E:\JDOM_Tutorial\Examples>java Test test.xml
************JDOM Exception *********
toString: org.jdom.input.JDOMParseException: Error on line 3 of document file:/E
:/JDOM_Tutorial/Examples/test.xml: cvc-datatype-valid.1.2.1: 'E:\JDOM_Tutorial\E
xamples\test.xsd' is not a valid value for 'anyURI'.
getMessage(): Error on line 3 of document file:/E:/JDOM_Tutorial/Examples/test.x
ml: cvc-datatype-valid.1.2.1: 'E:\JDOM_Tutorial\Examples\test.xsd' is not a vali
d value for 'anyURI'.
getCause() org.xml.sax.SAXParseException: cvc-datatype-valid.1.2.1: 'E:\JDOM_Tut
orial\Examples\test.xsd' is not a valid value for 'anyURI'.
************************************
E:\JDOM_Tutorial\Examples>
Would you please help me find the problem?
Best regards,
AG
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- XML schema for Product Catalog (BRD) Validator elementFormDefault="qualified" -->
<SynchData xsi:noNamespaceSchemaLocation="E:\JDOM_Tutorial\Examples\test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Type>ORACLE</Type>
</SynchData>
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- XML schema for Product Catalog (BRD) Validator elementFormDefault="qualified" -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:simpleType name="DatabaseType">
<xs:restriction base="xs:string">
<xs:enumeration value="ORACLE"/>
<xs:enumeration value="SYBASE"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="Type" type="DatabaseType"/>
<xs:element name="SynchData">
<xs:complexType>
<xs:sequence>
<xs:element ref="Type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>