Java XML Schemavalidation:前缀未绑定
我已经按照本教程validation了XML文件。 但是在validationXML文件时我收到了exception。 我做错了什么? 我的代码:
XML架构:
要validation的XML文件:
FirstName Lastname +xxxxxxxxxxxx
用于validation的Java源代码:
import javax.xml.XMLConstants; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.*; import org.xml.sax.SAXException; import java.io.*; public class ProtocolValidator { public static void main(String [] args) throws Exception { Source schemaFile = new StreamSource(new File("schema.xsd")); Source xmlFile = new StreamSource(new File("test_xml.xml")); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(schemaFile); Validator validator = schema.newValidator(); try{ validator.validate(xmlFile); System.out.println(xmlFile.getSystemId() + " is valid"); } catch (SAXException e) { System.out.println(xmlFile.getSystemId() + " is NOT valid"); System.out.println("Reason: " + e.getLocalizedMessage()); } } }
我收到的例外情况:
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/root/test/schema.xsd; lineNumber: 4; columnNumber: 50; The prefix "xs" for element "xs:element" is not bound. at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)...
XML模式文件本身需要是有效的XML文档。 您缺少xs
前缀的外部架构元素和名称空间声明。
将此行添加到您的架构中,就在第一行的下方:
…和结束标记作为模式中的最后一行: