Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在servlet上下文中从jar文件读取xml模式_Java_Xml_Servlets_Jar_Schema - Fatal编程技术网

Java 如何在servlet上下文中从jar文件读取xml模式

Java 如何在servlet上下文中从jar文件读取xml模式,java,xml,servlets,jar,schema,Java,Xml,Servlets,Jar,Schema,我有一个maven库项目,其中包含一些处理xml消息的类。每当我收到其中一条消息时,我都会使用我编写的xml模式文件对其进行验证。为我执行验证的代码如下所示: public static Document parseXML(final String xml) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser", true)

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

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));
}
private static final String XML_SCHEMA_LOCATION = "/ConvertMessageProtocol.xsd";
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\u SCHEMA\u LOCATION
是这样的:

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));
}
private static final String XML_SCHEMA_LOCATION = "/ConvertMessageProtocol.xsd";
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();
        }
    }
}
.xsd文件位于
src/main/resources
中,多亏了maven,一切工作都很完美:当告诉maven制作包时,.xsd文件包含在.jar中。 我做了一个简单的测试项目来检查是否能找到.xsd文件。源代码如下所示:

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));
}
private static final String XML_SCHEMA_LOCATION = "/ConvertMessageProtocol.xsd";
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将使用模式文件进行验证

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

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

你有什么想法吗?这里怎么了

提前感谢:

吉姆分配

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

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

问题是您的servlet应用程序正在查找没有文件的
/
处的文件。

非常感谢您的快速回答。但要么我不明白,要么我没有说清楚:
XML\u SCHEMA\u位置
和对
getResource
的调用都在同一个
MyMessage
类中。在您的jar文件中您正在使用
私有静态最终字符串XML\u SCHEMA\u LOCATION=“/ConvertMessageProtocol.xsd”从
/
读取配置文件
现在您已经在web应用程序中添加了该jar,因此现在
/
将与您现在需要将其视为资源的情况不同。可以使用上述方法,也可以将
私有静态最终字符串XML_SCHEMA_LOCATION=“/ConvertMessageProtocol.xsd”
替换为
“classpath:ConvertMessageProtocol.xsd”
是的,非常感谢,我花了好几个小时试图找到解决方案。我用getPath方法尝试了第一个,效果很好。