-- 作者:暮月游人
-- 发布时间:8/23/2009 3:13:00 PM
-- [求助]关于用JAVA通过xsd验证xml的问题
老是报错 Error on line 2: cvc-elt.1: Cannot find the declaration of element 'school'. 以下是源代码 xml <?xml version="1.0" encoding="UTF-8"?> <school xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\eclipse3.4\school\school.xsd"> <student> <name>22</name> <age>22</age> <gender>22</gender> <class>22</class> </student> </school> xsd <?xml version="1.0" encoding="UTF-8" ?> - <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> - <xs:element name="school"> - <xs:complexType> - <xs:sequence> - <xs:element name="student"> - <xs:complexType> - <xs:sequence> <xs:element name="name" type="xs:string" id="name" /> <xs:element name="age" type="xs:int" id="age" /> <xs:element name="gender" type="xs:int" id="gender" /> <xs:element name="class" type="xs:int" id="class" /> </xs:sequence> <xs:attribute name="ID" type="xs:int" id="ID" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> java程序 import java.io.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; public class XMLTest{ String xml="school.xml"; String schema="school.xsd"; public void validate() { try{ SAXBuilder saxBuilder = new SAXBuilder(true); saxBuilder.setValidation(true); saxBuilder.setFeature("http://apache.org/xml/features/validation/schema", true); saxBuilder.setProperty( "http://apache.org/xml/properties/schema/" + "external-noNamespaceSchemaLocation",schema); Document jdomDoc = saxBuilder.build(new FileInputStream(new File(xml))); XMLOutputter xmlOutputter = new XMLOutputter(); //xmlOutputter.setEncoding("gb2312"); xmlOutputter.output(jdomDoc, System.out); }catch(Exception e){ System.out.println("验证失败:"+e); } } public static void main(String args[]){ new XMLTest().validate(); } }
|