基于JDOM,用Schema验证XML的有效性老是出现这样的异常,帮帮忙!

languagedy 2005-07-26 09:53:34
我基于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);
}

}

谁能帮帮我?
...全文
147 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
languagedy 2005-07-26
我的异常如下:
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)
回复
zzzle 2005-07-26
感觉是你XSD写得不对
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2005-07-26 09:53
社区公告
暂无公告