基于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);
}

}

谁能帮帮我?
...全文
179 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
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写得不对

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧