尝试使用XSD文件验证XML时出现“文件未找到异常”

3

当我尝试运行以下代码时,我一直收到“文件未找到”异常:

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;
    }

}

这是我调用方法的方式:
Boolean value = validateXMLSchema("shiporder.xsd",xmlfile);

shiporder是编译器在项目文件夹中寻找的文件名。

xmlfile变量是一个包含将与xds文件进行比较的xml文件的字符串。

尽管我已经检查了文件位置正确,但我仍然遇到了文件未找到异常。

这是我的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>

这是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>

有人知道为什么我会遇到这个问题吗?


1
new File(xmlPath).exists()new File(xsdPath).exists()返回什么?(完整的堆栈跟踪也会很有帮助。)如果上述任何一个返回false,那么你就找到了问题所在 - 如果是这种情况,请打印出相关文件的getAbsolutePath()并检查它与您期望的差异。 - Michael Berry
1个回答

1

您没有告诉File()构造函数文件在哪里。该构造函数将把文件名解析为一个抽象路径名,并将结果与系统相关的默认目录进行解析。这可能不是您想要的。

如果xsd文件在您的项目中的某个位置,请使用yourProject.YourReader.class.getResource(xsdPath)。xsdPath将是一个“相对”资源名称,它被视为相对于类的包。或者,您可以使用前导斜杠指定“绝对”资源名称。例如,如果您的xsd文件与其读取器在同一目录中,请使用getResource("shiporder.xsd")。如果您从项目根目录开始,请使用getResource("/path/to/shiporder.xsd")

然后,如果需要,您可以使用new File(resource.toURI())将此资源转换为文件。

对于您的代码:

    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());
                ...
    }}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接