如何从servlet上下文中的jar文件中读取xml架构 [英] How to read xml schema from jar file in a servlet context

查看:141
本文介绍了如何从servlet上下文中的jar文件中读取xml架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个maven库项目,其中包含一些处理xml消息的类。每当我收到其中一条消息时,我都会使用我编写的xml架构文件对其进行验证。对我进行验证的代码如下所示:

I have a maven library project with some classes to deal xml messages. Whenever I receive one of those messages I validate it using an xml schema file I have written. The code that does the validation for me looks like this:

public static Document parseXML(final String xml) throws JDOMException, IOException {
    SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
    builder.setFeature("http://apache.org/xml/features/validation/schema", true);
    URL location = CMPMessage.getClass().getResource(XML_SCHEMA_LOCATION);
    if (null == location) {
        throw new IOException("Unable to load schema definition file.");
    }
    builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
            "http://www.mycompany.de/MyProtocol " + location);
    return builder.build(new StringReader(xml));
}

XML_SCHEMA_LOCATION 是像这样:

private static final String XML_SCHEMA_LOCATION = "/ConvertMessageProtocol.xsd";

.xsd文件位于 src / main / resources 并且,感谢maven,一切都很完美:当告诉maven制作包时,.xsd文件会包含在.jar中。
我做了一个简单的测试项目来检查是否真的会找到.xsd文件。源代码如下所示:

The .xsd file is in src/main/resources and, thanks to maven, everything works perfect: The .xsd file gets included in the .jar when tell maven to make a package. I made a simple test project to check whether the .xsd file will actually be found. The source looks like this:

import java.io.IOException;
import org.jdom.JDOMException;
import de.mycomp.MyMessage;


public class Main {
    public static void main(final String[] args) {
        try {
            MyMessage.parseXML(args[0]);
        }
        catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

是的,xml会使用模式文件进行验证。

And yes, the xml will be validated using the schema file.

现在它来了:我想在servlet中使用我的小库(在tomcat中运行),但在那里,.xsd文件无法找到:(

Now here it comes: I want to use my little library in a servlet (running in tomcat), but there, the .xsd file can not be found :(

当然,我可以将.xsd文件直接存储在公司服务器上,并通过http检索它,但我认为包括它在.jar中是更好的解决方案,以确保libs和模式版本适合。

Of course, I could store the .xsd file somewhere else like directly on the companys server and retrieve it via http, but I think including it in the .jar is the better solution to make sure that the libs and the schemas version fit.

你有什么想法吗?

提前致谢:

Jim

推荐答案

assign

private static final String XML_SCHEMA_LOCATION = getPath("/ConvertMessageProtocol.xsd");

public static String getPath(String path){
  return UtilityClass.class.getResource(path).toString();
}

问题在于你的服务vlet app正在 / 中查找没有文件的文件。

The problem is your servlet app is looking for file at / where there is no file.

这篇关于如何从servlet上下文中的jar文件中读取xml架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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