JAXP-调试XSD目录查找 [英] JAXP - debug XSD catalog look up

查看:86
本文介绍了JAXP-调试XSD目录查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的情况是,我们要针对放在文件系统中的XSD来验证作为字节流保存在内存中的XML文档.我们希望避免在XML文件中明确提及文件名,而是告诉XML解析器使用一个或多个XSD文件的目录进行验证.

I have a situation where we want to validate an XML document held as a byte stream in memory, against an XSD placed amongst others in a file system. We would like to avoid having the file name explicitly mentioned in the XML file but instead tell the XML parser to use a catalog of one or more XSD files for validation.

我尝试创建DocumentBuilder提供程序(用于Guice 3.0)如下:

My attempt to create a DocumentBuilder provider (for Guice 3.0) looks like:

public class ValidatingDocumentBuilderProvider implements
        Provider<DocumentBuilder> {

    static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";

    Logger log = getLogger(ValidatingDocumentBuilderProvider.class);

    DocumentBuilderFactory dbf;

    public synchronized DocumentBuilder get() { // dbf not thread-safe

        if (dbf == null) {
            log.debug("Setting up DocumentBuilderFactory");

            // http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPDOM8.html
            dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setValidating(true);
            dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            // parser should look for schema reference in xml file

            // Find XSD's in current directory.

            FilenameFilter fileNameFilter = new FilenameFilter() {

                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".xsd");
                }
            };
            File[] schemaFiles = new File(".").listFiles(fileNameFilter);

            dbf.setAttribute(JAXP_SCHEMA_SOURCE, schemaFiles);

            log.debug("{} schema files found", schemaFiles.length);
            for (File file : schemaFiles) {
                log.debug("schema file: {}", file.getAbsolutePath());
            }

        }

        try {
            return dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("get DocumentBuilder", e);
        }
    }
}

(我也尝试过使用文件名). Eclipse接受XSD-放入目录中时,它可以验证此处处理的XML

(and I have also tried with file names too). Eclipse accepts the XSD - when put in the catalog it can validate the XML dealt with here

在试图进行验证时,解析器会短暂地暂停以肉眼可见的方式.这可能是网络查找.

It appears to the naked eye that the parser halts briefly when trying to validate. This might be a network lookup.

-Djaxp.debug=1仅添加这些行

JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl using ClassLoader: null

如何在JDK 6中获得解析器以告诉我它在做什么?如果无法执行此操作,如何检查其中的XML Catalog使用情况,以查看为什么未选择提供的XSD?

How can I get the parser in JDK 6 to tell me what it is doing? If I cannot do that, how do I inspect the XML Catalog usage inside it to see why the XSDs provided are not selected?

我忽略了哪些明显的事情?

What obvious thing have I overlooked?

推荐答案

您说

我们希望避免在XML文件中明确提及文件名

We would like to avoid having the file name explicitly mentioned in the XML file

那么解析器将如何选择合适的模式?

How then would the parser be able to select the appropriate schema?

您可以尝试的是,基于所有可用的架构资源,使用SchemaFactory创建一个Schema,并将其附加到文档构建器工厂.然后,解析器将针对此超级模式"自动验证文档.

What you can try, is to create a Schema, using a SchemaFactory, based on all the available schema resources and attach that to the document builder factory. The parser will then automatically validate the document against this "super schema".

如果您的模式集具有内部依赖性(即导入或包含),请确保使用相对URL或专用解析器正确解析了这些引用.

If your set of schemas has internal dependencies (i.e. import or include), make sure that those references are resolved correctly using relative URLs or a specialised resolver.

更新:

阅读此书后, http://java.sun. com/j2ee/1.4/docs/tutorial/doc/JAXPDOM8.html ,请多加注意,我意识到您的方法应与我的建议具有相同的效果,因此还有其他事情要进行.我只能说我所描述的效果很好.

After reading this, http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM8.html, a bit more carefully, I realize that you approach should have the same effect as my proposal, so something else is going n. I can only say that what I describe works very well.

这篇关于JAXP-调试XSD目录查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆