使用xs验证与xsd架构的问题

时间:2015-05-29 00:22:07

标签: java xml validation xsd

这是我第一次使用xsd来验证java中的xml文档,而我似乎无法让它工作。

嗯,首先这里是我的xml文档结构,Employees.xml:

<?xml version="1.0"?>
<x:Employees xmlns:x="urn:Employees">
  <Employee id="1">
    <name>Some Guy</name>
    <gender>Male</gender>
    <role>Java Developer</role>
  </Employee>
  <Employee id="2">
    <name>Some lady</name>
    <gender>Female</gender>
    <role>CEO</role>
  </Employee>
</x:Employees>

这是我的xsd文件结构,EmployeesXmlSchema.xsd:

<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:x="urn:Employees" targetNamespace="urn:Employees">

<xsd:element name="Employees" type="x:Employees"/>
<xsd:complexType name="Employees">
<xsd:sequence>
    <xsd:element name="Employee" type="x:Employee" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Employee">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="gender" type="xsd:string"/>
        <xsd:element name="role" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

这是java代码,试图验证xml文档:

try {
            String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
            SchemaFactory factory = SchemaFactory.newInstance(language);
            schema = factory.newSchema(schemaFile);

            // Parse the XML document to DOM Document, and then validate it
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            builderFactory.setNamespaceAware(true);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document doc = builder.parse(xmlFile);
            DOMSource source = new DOMSource(doc);

            Validator validator = schema.newValidator();
            validator.validate(source);

            return true;

        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }

我得到的错误是SAXParseException,并说:

  

从元素'Employee'开始发现无效内容。之一   '{“urn:Employees”:Employee}'是预期的。

我真的希望有人可以向我解释为什么会出错。

任何帮助都将受到非常感谢

1 个答案:

答案 0 :(得分:0)

首先,您应该更正您的XML:

<Employees xmlns="urn:Employees" id="1">
    <name>Some Guy</name>
    <gender>Male</gender>
    <role>Java Developer</role>
</Employees>

或更正您的XSD:

<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:x="urn:Employees" targetNamespace="urn:Employees">

<xsd:element name="Employees" type="x:Employees"/>
<xsd:complexType name="Employees">
<xsd:sequence>
    <xsd:element name="Employee" type="x:Employee" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Employee">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string"/>
        <xsd:element name="gender" type="xsd:string"/>
        <xsd:element name="role" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

其次,您应该启用对XML命名空间的支持:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);