-- 作者:znjabc123
-- 发布时间:7/26/2005 10:06:00 AM
-- 基于JDOM,用Schema验证XML的有效性老是出现这样的异常,帮帮忙!
我基于JDOM,用Schema验证XML的有效性老是出现这样的异常,帮帮忙! 我的XML如下: <?xml version="1.0" encoding="UTF-8"?> <!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by Ke Deng (Sun) --> <Questions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Question ID="1" Date="2002-03-18" Section="java.awt package" Type="Choicer"> <Answer> <Item IsTrue="true">selrjweoriw</Item> </Answer> <Content>sdfwer</Content> <Comment>rwrwe</Comment> </Question> </Questions> 我的XSD如下: <?xml version="1.0" encoding="UTF-8"?> <!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by Ke Deng (Sun) --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="Questions"> <xs:annotation> <xs:documentation>Comment describing your root element</xs:documentation> </xs:annotation> <xs:complexType> <xs:all> <xs:element ref="Question"/> </xs:all> </xs:complexType> </xs:element> <xs:element name="Question"> <xs:complexType> <xs:sequence> <xs:element ref="Answer"/> <xs:element name="Content"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Comment" type="xs:string"/> </xs:sequence> <xs:attribute name="ID" type="xs:int" use="required"/> <xs:attribute name="Provider" type="xs:string" use="optional"/> <xs:attribute name="Date" type="xs:date" use="required"/> <xs:attribute name="Section" type="xs:string" use="required"/> <xs:attribute name="Type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Choicer"/> <xs:enumeration value="Filler"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="Answer"> <xs:complexType> <xs:all> <xs:element name="Item"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="IsTrue" type="xs:boolean" use="optional" fixed="true"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:all> </xs:complexType> </xs:element> </xs:schema> 我的JAVA编码如下: package com.gefon.xml.test1; import org.apache.xerces.parsers.SAXParser; import org.jdom.Document; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; public class JDOMXMLSchemaValidatorTest { public static void main(String args[]) { try { JDOMXMLSchemaValidatorTest test = new JDOMXMLSchemaValidatorTest () ; test.test1() ; } catch(Exception e) { e.printStackTrace() ; } } public void test1() throws Exception { SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); saxBuilder.setValidation(true); saxBuilder.setFeature("http://apache.org/xml/features/validation/schema", true); saxBuilder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","file:///C:\\WorkRoom\\1.xsd"); Document jdomDoc = saxBuilder.build("file:///C:\\WorkRoom\\test.xml"); XMLOutputter xmlOutputter = new XMLOutputter(); xmlOutputter.output(jdomDoc, System.out); } } 我的异常如下: org.jdom.input.JDOMParseException: Error on line 3 of document file:///C:/WorkRoom/test.xml: cvc-elt.1: Cannot find the declaration of element 'Questions'. at org.jdom.input.SAXBuilder.build(SAXBuilder.java:465) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891) at com.gefon.xml.test1.JDOMXMLSchemaValidatorTest.test1(JDOMXMLSchemaValidatorTest.java:34) at com.gefon.xml.test1.JDOMXMLSchemaValidatorTest.main(JDOMXMLSchemaValidatorTest.java:17) Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Questions'. at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453) ... 3 more Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Questions'. at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891) at com.gefon.xml.test1.JDOMXMLSchemaValidatorTest.test1(JDOMXMLSchemaValidatorTest.java:34) at com.gefon.xml.test1.JDOMXMLSchemaValidatorTest.main(JDOMXMLSchemaValidatorTest.java:17) Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Questions'. at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:453) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:891) at com.gefon.xml.test1.JDOMXMLSchemaValidatorTest.test1(JDOMXMLSchemaValidatorTest.java:34) at com.gefon.xml.test1.JDOMXMLSchemaValidatorTest.main(JDOMXMLSchemaValidatorTest.java:17)
|