Reputation: 181
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.
The xmlfile variable is a string containing the xml file which will be compared against the xds file.
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 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:
<?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?
Upvotes: 3
Views: 874
Reputation: 21
You're not telling the File() constructor where the file is. 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)
. xsdPath will be a "relative" resource name, which is treated relative to the class's package. 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")
. If you are going from the project root, use getResource("/path/to/shiporder.xsd")
.
You can then turn this resource into a File, if you need to, by using 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());
...
}}
Upvotes: 1