加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java XML Schema验证:前缀未绑定

发布时间:2020-12-14 05:50:57 所属栏目:Java 来源:网络整理
导读:我已经遵循了 this tutorial来验证XML文件.但是在验证XML文件时收到异常.我做错了什么?我的代码: XML模式: ?xml version="1.0" encoding="utf-8" ?!-- definition of simple elements --xs:element name="first_name" type="xs:string" /xs:element name=
我已经遵循了 this tutorial来验证XML文件.但是在验证XML文件时收到异常.我做错了什么?我的代码:
XML模式:
<?xml version="1.0" encoding="utf-8" ?>

<!-- definition of simple elements -->
<xs:element name="first_name" type="xs:string" />
<xs:element name="last_name" type="xs:string" />
<xs:element name="phone" type="xs:string" />

<!-- definition of attributes -->
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="date" type="xs:date" use="required"/>

<!-- definition of complex elements -->

<xs:element name="reporter">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="first_name" />
            <xs:element ref="last_name" />
            <xs:element ref="phone" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="report">
    <xs:complexType>
        <xs:attribute ref="type"/>
        <xs:attribute ref="date" />
        <xs:sequence>
            <xs:element ref="reporter" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

要验证的XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<report type="5" date="2012-12-14">
    <reporter>
        <first_name>FirstName</firstname>
        <last_name>Lastname</lastname>
        <phone>+xxxxxxxxxxxx</phone>
    </reporter>
</report>

用于验证的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前缀的命名空间声明.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- schema elements here -->
</xs:schema>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读