HI我正在尝试使用JDOM使用xsd验证xml。 这是我的验证代码: -
public class jdom1 {
public static void main(String[] args) throws JDOMException, IOException {
// TODO Auto-generated method stub
File xsdfile = new File("file.xsd");
XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
SAXBuilder builder = new SAXBuilder(schemafac);
Document doc = builder.build(new File("file.xml"));
Element root = doc.getRootElement();
for(Element testCase : root.getChildren()){
//Code
}
}
我的file.xml: -
<?xml version="1.0" encoding="UTF-8"?>
<document>
<testCaseDataName>
<id>1</id >
<testCaseName>Edit</testCaseName >
<expectedResult>true</expectedResult >
</testCaseDataName>
<testCaseDataName>
<id>2</id >
<testCaseName>add</testCaseName >
<expectedResult>false</expectedResult>
<parameter>
<key>featues</key >
<value>w,f</value>
</parameter>
</testCaseDataName>
<testCaseDataName>
<id>3</id >
<testCaseName>delete</testCaseName >
<expectedResult>duplicate</expectedResult>
<parameter>
<key>projectType</key >
<value>new</value>
<key>Name</key >
<value>mashnew</value>
<key>status</key >
<value>ACTIVE</value>
<key>canOrder</key >
<value>Yes</value>
</parameter>
</testCaseDataName>
</document>
我的file.xsd: -
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/ACL"
xmlns:tns="http://www.example.org/ACL"
elementFormDefault="qualified">
<include schemaLocation=""></include>
<element name="document">
<complexType>
<sequence>
<element name="testCaseDataName">
<complexType>
<sequence>
<element name ="id" type ="ID" ></element>
<element name ="testCaseName" type ="string" ></element>
<element name = "expectedResult" type="string"></element>
<element name = "parameter" minOccurs="0">
<complexType>
<sequence>
<element name="key" type ="string" maxOccurs="1"></element>
<element name="value" type="string" maxOccurs="unbounded"></element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
当我运行此代码时,我收到以下错误。任何人都可以帮我解决这个问题。
Exception in thread "main" org.jdom2.input.JDOMParseException: Error on line 4 of document file:/C:/Users/file.xml: cvc-elt.1: Cannot find the declaration of element 'document'.
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:228)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:277)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:264)
at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1116)
at com.memoir.client.test.testdb.jdom1.main(jdom1.java:24)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'document'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
... 4 more
答案 0 :(得分:1)
这是验证的方法,请检查您的代码
SchemaFactory schemafac =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemafac.newSchema(new File("myschema.xsd"));
XMLReaderJDOMFactory factory = new XMLReaderSchemaFactory(schema);
SAXBuilder sb = new SAXBuilder(factory);
Document doc = sb.build(new File("file.xml"));
使用以下架构
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element name="testCaseDataName" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:byte" name="id"/>
<xs:element type="xs:string" name="testCaseName"/>
<xs:element type="xs:string" name="expectedResult"/>
<xs:element name="parameter" minOccurs="0">
<xs:complexType>
<xs:choice maxOccurs="unbounded" minOccurs="0">
<xs:element type="xs:string" name="key"/>
<xs:element type="xs:string" name="value"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答案 1 :(得分:0)
我的架构错了。使用此http://www.freeformatter.com/xsd-generator.html生成架构,验证工作正常。感谢