我目前正在努力解决编写一些单元测试以检查XSD是否对样本XML文件有效的问题。我收到了以下错误:
org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'VMS'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.beginNode(DOMValidatorHelper.java:277)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:244)
at com.sun.org.apache.xerces.internal.jaxp.validation.DOMValidatorHelper.validate(DOMValidatorHelper.java:190)
at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl.validate(ValidatorImpl.java:109)
我的XSD文件:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Messages header -->
<xs:complexType name="VMS_TYPE">
<!-- Recipient of message -->
<xs:attribute name="TO" type="xs:string"/>
<!-- Sender of message -->
<xs:attribute name="FR" type="xs:string" />
<!-- Message identification number consists -->
<xs:attribute name="VN" type="xs:string" />
<!-- Message creation date -->
<xs:attribute name="OD" type="xs:date" />
<!-- Message creation time -->
<xs:attribute name="OT" type="xs:string" />
<!-- Mean of communication to sender of message -->
<xs:attribute name="MTO" type="xs:string" />
<!-- Mean of communication to recipient of message -->
<xs:attribute name="MFR" type="xs:string" />
<!-- TODO - explain attribute meaning -->
<xs:attribute name="MD" type="xs:string" />
</xs:complexType>
</xs:schema>
第二
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="vms_header.xsd" />
<!-- Alarm message -->
<xs:complexType name="ALR_TYPE" >
<xs:complexContent>
<xs:extension base="VMS_TYPE">
<xs:sequence>
<xs:element name="ALR" type="ALR_BODY_TYPE" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ALR_BODY_TYPE">
<xs:attribute name="BAT" type="xs:int"/>
<xs:attribute name="TEM" type="xs:int"/>
<xs:attribute name="GPS" type="xs:int"/>
<xs:attribute name="SOS" type="xs:int"/>
<xs:attribute name="CV" type="xs:int"/>
<xs:attribute name="MOD" type="xs:int"/>
<xs:attribute name="FGPS" type="xs:int"/>
<xs:attribute name="FBAT" type="xs:int"/>
<xs:attribute name="FALM" type="xs:int"/>
<xs:attribute name="FSOS" type="xs:int"/>
<xs:attribute name="FFW" type="xs:int"/>
<xs:attribute name="ASOS" type="xs:int"/>
</xs:complexType>
</xs:schema>
第三
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="types/alr_msg_type.xsd"/>
<xs:element name="VMS" type="ALR_TYPE"/>
</xs:schema>
我的XML示例文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VMS TO="CV0001" FR="CAR001" VN="CAR001201406040001" OD="2014-06-04" OT="12:14" MD="+3465104505050">
<ALR BAT="0" TEM="0" GPS="0" SOS="0" CV="0" MOD="0" FGPS="0" FBAT="0" FALM="0" FSOS="0" FFW="0" ASOS="0"/>
</VMS>
答案 0 :(得分:1)
这对我有用,jdk1.8.0_20
./third.xsd # probably not the real name, but you no tell
./types/alr_msg_type.xsd
./types/vms_header.xsd
public static void main( String[] args ) throws Exception {
Handler handler = new Handler();
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
parser.setErrorHandler( handler );
try {
Document document = parser.parse(new File("x.xml"));
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(new File("third.xsd"));
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.setErrorHandler( handler );
// validate the DOM tree
try {
validator.validate(new DOMSource(document));
} catch (SAXException e) {
// ...
System.out.println( "VAlidation error" );
}
} catch (SAXParseException e) {
// syntax error in XML document
System.out.println( "Syntax error" );
}
}
答案 1 :(得分:0)
我使用了以下代码(使用jdk 1.8.0_11):
DefaultHandler handler = new DefaultHandler();
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
parser.setErrorHandler( handler );
try {
Document document = parser.parse(getClass().getResourceAsStream("x.xml"));
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaFile = new StreamSource(getClass().getResourceAsStream("third.xsd"));
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.setErrorHandler( handler );
// validate the DOM tree
try {
validator.validate(new DOMSource(document));
} catch (SAXException e) {
// ...
System.out.println( "Validation error" );
}
} catch (SAXParseException e) {
// syntax error in XML document
System.out.println( "Syntax error");
System.out.println(e);
}
结果是:
语法错误 org.xml.sax.SAXParseException; lineNumber:6; columnNumber:45; src-resolve:无法解析名称&#39; ALR_TYPE&#39;到(n)&#39;类型定义&#39;成分