小编Ken*_*ned的帖子

带有自定义资源解析器的 Java XML 模式验证器无法解析元素

Java XML Schema 验证 - ResourceResolver 问题

我正在尝试将一个组件构建到一个 Web 应用程序中,该应用程序将根据一组模式验证不同的 XML 文档。

我在 java 包 com.example.xml 中有验证器类,然后我有一个模式 com.example.xml.Schemas 的“包”,它们根据命名空间以及任何包含的模式进行组织,如下所示:

com.example.xml/SchemaValidator.java

com.example.xml.Schemas/2008/07/05/Message.xsd
com.example.xml.Schemas/2008/07/05/Person.xsd
com.example.xml.Schemas/2008/07/05/Address.xsd

com.example.xml.Schemas/2010/09/21/Message.xsd
com.example.xml.Schemas/2010/09/21/Organization.xsd
com.example.xml.Schemas/2010/09/21/Person.xsd
com.example.xml.Schemas/2010/09/21/Address.xsd
Run Code Online (Sandbox Code Playgroud)

所以大多数文档元素都被称为 Message 但在不同的命名空间和不同的内部结构中。通过映射,我可以确定正确架构的位置,然后加载与根元素名称匹配的 XSD - 但为了使解析器能够读取包含的架构,我尝试使用 org.w3c.dom.ls .LSResourceResolver 实现以根据 systemId 获取正确的文件。

public class ResourceResolver implements LSResourceResolver {
    private String basePath;

    public ResourceResolver( String baseDirectory ){
        basePath = baseDirectory;
        if( !basePath.endsWith("/")){
            basePath += "/";
        }
    }

    @Override
    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
        System.out.println("Resolving: " + type …
Run Code Online (Sandbox Code Playgroud)

java xml eclipse xsd

5
推荐指数
0
解决办法
2916
查看次数

标签 统计

eclipse ×1

java ×1

xml ×1

xsd ×1