-- 作者:chenbo980
-- 发布时间:4/9/2007 4:07:00 PM
-- 请帮忙,有关xsd验证xml文件的疑问!
xsd文件如下: <?xml version="1.0" encoding="GBK"?> <!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by () --> <xs:schema xmlns="http://www.chinatax.gov.cn/dataspec/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.chinatax.gov.cn/dataspec/" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:include schemaLocation="taxDoc.xsd"/> <xs:include schemaLocation="taxMLpublic.xsd"/> <xs:include schemaLocation="taxMLSkyPublic.xsd"/> <xs:complexType name="jhSkySsKkReqest"> <xs:annotation> <xs:documentation>实时扣款请求报文</xs:documentation> </xs:annotation> <xs:complexContent> <xs:extension base="taxDoc"> <xs:sequence> <xs:element name="ssKkItem" type="jhSkySsKkReqBody"> <xs:annotation> <xs:documentation>实时扣款信息</xs:documentation> </xs:annotation> </xs:element> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="jhSkySsKkReqBody"> <xs:annotation> <xs:documentation>实时扣款信息</xs:documentation> </xs:annotation> <xs:sequence> <xs:element name="kkXx" type="jhSkySsKkYwxxReq"> <xs:annotation> <xs:documentation>扣款信息</xs:documentation> </xs:annotation> </xs:element> <xs:element name="ywys" type="ywys"> <xs:annotation> <xs:documentation>业务要素</xs:documentation> </xs:annotation> </xs:element> </xs:sequence> </xs:complexType> </xs:schema> 根据这个文件生成的xml文件如下: <ssKkItem xmlns="http://www.chinatax.gov.cn/dataspec/"> <kkXx> <spxx> <sptxx> <xtsphm>asdf</xtsphm> <nsrsbh>asdf</nsrsbh> <nsrmc>asdf</nsrmc> <zsjgDm>c</zsjgDm> <zsjgQc>asdf</zsjgQc> <jkrMc/> <yhzhxx> <yhzlDm>1234</yhzlDm> <yhDm>boc</yhDm> <yhmc>中国银行</yhmc> <yhzh>888888-888</yhzh> </yhzhxx> <skgkDm>asdf</skgkDm> <yszl>a</yszl> <zlqbz>b</zlqbz> <skgkMc>asdf</skgkMc> <kpje>10000.0</kpje> <djzclxDm>c</djzclxDm> <bz1/> <bz2>asdf</bz2> </sptxx> <kprq>20070328</kprq> <szmx> <xh>1</xh> <yskmDm>ssss</yskmDm> <spjkqx>20070320</spjkqx> <sssq> <rqQ>20070201</rqQ> <rqZ>20070228</rqZ> </sssq> <szje>1234.0</szje> <zsxmDm>s</zsxmDm> <zsxmMc>ssss</zsxmMc> <smxx> <xh>1</xh> <zspmDm>11</zspmDm> <zspmMc>smxx111</zspmMc> <sjse>10000.0</sjse> <kssl>1000.0</kssl> <sl>0.22</sl> <yjhkcje>100.0</yjhkcje> </smxx> <smxx> <xh>1</xh> <zspmDm>11</zspmDm> <zspmMc>smxx111</zspmMc> <sjse>10000.0</sjse> </smxx> <param> <name>x</name> <value>rrrssss</value> </param> <param> <name>y</name> <value>ggggggg</value> </param> </szmx> <szmx> <xh>1</xh> <yskmDm>ssss</yskmDm> <spjkqx>20070320</spjkqx> <sssq> <rqQ>20070201</rqQ> <rqZ>20070228</rqZ> </sssq> <szje>1234.0</szje> <zsxmDm>s</zsxmDm> <zsxmMc>ssss</zsxmMc> <smxx> <xh>1</xh> <zspmDm>11</zspmDm> <zspmMc>smxx111</zspmMc> <sjse>10000.0</sjse> </smxx> </szmx> <szmx> <xh>1</xh> <yskmDm>cccc</yskmDm> <spjkqx>20070320</spjkqx> <sssq> <rqQ>20070203</rqQ> <rqZ>20070228</rqZ> </sssq> <szje>1234.0</szje> <zsxmDm>sss</zsxmDm> <zsxmMc>test</zsxmMc> <smxx> <xh>1</xh> <zspmDm>11</zspmDm> <zspmMc>smxx111</zspmMc> <sjse>10000.0</sjse> </smxx> <param> <name>x</name> <value>rrrssss</value> </param> <param> <name>y</name> <value>ggggggg</value> </param> </szmx> </spxx> <qsyhxx> <sfxysh>aaaaaaaaaaaaaa</sfxysh> <qsyhhh>asdf222222</qsyhhh> </qsyhxx> </kkXx> <ywys> <zsjgDm>c</zsjgDm> <wtrq>20070330</wtrq> <jylsh>CD1</jylsh> </ywys> </ssKkItem> 使用的验证方法: public static boolean isXmlOK(String xsdFile, byte[] tagetXml) throws SAXException, IOException, DocumentException { boolean flag = true; SAXReader reader = new SAXReader(); reader.setValidation(true); reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", xsdFile); //reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "templateXMLSchema.xsd"); XMLErrorHandler errorHandler = new XMLErrorHandler(); reader.setErrorHandler(errorHandler); InputStream in = new ByteArrayInputStream(tagetXml); InputStreamReader utf8In=new InputStreamReader(in,"utf-8"); //Document document = reader.read(in); reader.read(utf8In); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); if (errorHandler.getErrors().hasContent()) { flag = false; writer.write(errorHandler.getErrors()); } else { System.out.println("Xml is OK!."); } return flag; } 出现的错误为: <errors> <error column="56" line="1">cvc-elt.1: Cannot find the declaration of element 'ssKkItem'.</error> </errors> 请问各位高手是什么原因呢?帮一下忙嘛。谢谢
|