以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  基于JDOM,用Schema验证XML的有效性老是出现这样的异常,帮帮忙!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=20683)


--  作者: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)


--  作者:znjabc123
--  发布时间:7/27/2005 4:48:00 PM

--  
哇噻,气氛太低了!斑竹能否指点一下!
--  作者:stolang
--  发布时间:4/17/2006 7:06:00 PM

--  
我也碰到这个问题,怎么这么没人回答呀???
--  作者:SATOKO2006
--  发布时间:4/18/2006 9:15:00 AM

--  
有空研究研究
--  作者:SATOKO2006
--  发布时间:4/20/2006 11:33:00 AM

--  
试了,无问题啊,楼主是不是自己把验证的XML弄错了
--  作者:qingfeng53
--  发布时间:4/26/2006 10:01:00 AM

--  
我也通不过,我的jdom可能出问题了,不能用来解析xml
--  作者:java_liuhj
--  发布时间:5/30/2006 4:07:00 PM

--  
顶上,怎么没人会啊
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
187.500ms