简体   繁体   English

尝试针对XSD文件验证xml时找不到文件异常

[英]File not found exception when attempting to validate xml against xsd file

I keep getting File Not Found exception when I try to run the following code: 尝试运行以下代码时,我不断收到“找不到文件”异常:

public static boolean validateXMLSchema(String xsdPath, String xmlPath){
    try{
        SchemaFactory factory =
                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File(xsdPath));
        javax.xml.validation.Validator validator = schema.newValidator();
        validator.validate(new StreamSource(new File(xmlPath)));
        return true;
    }
    catch (Exception e){
        System.out.println(e);
        return false;
    }

}

This is how I'm calling my method: 这就是我调用我的方法的方式:

Boolean value = validateXMLSchema("shiporder.xsd",xmlfile);

shiporder is the name of the file that the compiler looks for in the project folder. shiporder是编译器在项目文件夹中查找的文件的名称。

The xmlfile variable is a string containing the xml file which will be compared against the xds file. xmlfile变量是包含将与xds文件进行比较的xml文件的字符串。

I'm getting a file not found exception even though I've checked the location of the file is correct. 即使我已检查文件的位置是否正确,也出现文件未找到异常。

This is my xml file: 这是我的xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
  <orderperson>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
  </item>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder>

This is the xsd file: 这是xsd文件:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

Does anyone know why I'm getting this issue? 有谁知道我为什么遇到这个问题?

You're not telling the File() constructor where the file is. 您没有告诉File()构造函数文件在哪里。 This constructor will resolve the filename to an abstract pathname and resolve the result against a system-dependent default directory. 此构造函数会将文件名解析为抽象路径名,并根据系统相关的默认目录解析结果。 That's probably not what you want. 那可能不是您想要的。

If the xsd is somewhere in your project, use yourProject.YourReader.class.getResource(xsdPath) . 如果xsd在项目中的某个位置,请使用yourProject.YourReader.class.getResource(xsdPath) xsdPath will be a "relative" resource name, which is treated relative to the class's package. xsdPath将是一个“相对”资源名称,相对于类的程序包而言。 Alternatively you can specify an "absolute" resource name by using a leading slash. 或者,您可以使用斜杠指定“绝对”资源名称。 For example, if your xsd is in the same directory as its reader, use getResource("shiporder.xsd") . 例如,如果您的xsd与阅读器位于同一目录中,请使用getResource("shiporder.xsd") If you are going from the project root, use getResource("/path/to/shiporder.xsd") . 如果要从项目根目录开始,请使用getResource("/path/to/shiporder.xsd")

You can then turn this resource into a File, if you need to, by using new File(resource.toURI()) 然后,您可以根据需要使用new File(resource.toURI())将此资源转换为文件。

For your code: 对于您的代码:

    public static boolean validateXMLSchema(String xsdPath, String xmlPath){
           try{
                SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = factory.newSchema(new File(ThisClass.class.getResource("/path/to/shiporder.xsd").toURI());
                ...
    }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM