logo       

invalid type error?: msg#00038

text.xml.xmlbeans.user

Subject: invalid type error?

I am evaluating XmlBeans to replace some existing code in a project, and I
am getting an error when attempting to validate some xml. I have modified
the purchase order example to match what I am doing.

We are not using a namespace in our xml, but we still specify a schema to
validate against:

<?xml version="1.0" encoding="UTF-8"?>
<purchase-order
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:noNamespaceSchemaLocation="po.xsd">
<customer>
<name>Gladys Kravitz</name>
<address>Anytown, PA</address>
</customer>
<date>2003-01-07T14:16:00-05:00</date>
<line-item>
<description>Burnham's Celestial Handbook, Vol 1</description>
<per-unit-ounces>5</per-unit-ounces>
<price>21.79</price>
<quantity>2</quantity>
</line-item>
<line-item>
<description>Burnham's Celestial Handbook, Vol 2</description>
<per-unit-ounces>5</per-unit-ounces>
<price>19.89</price>
<quantity>2</quantity>
</line-item>
<shipper>
<name>ZipShip</name>
<per-ounce-rate>0.74</per-ounce-rate>
</shipper>
</purchase-order>

The schema looks like:

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

<xs:element name="purchase-order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="customerType"/>
<xs:element name="date" type="xs:dateTime"/>
<xs:element name="line-item" type="line-itemType"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="shipper" type="shipperType"
minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="customerType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="line-itemType">
<xs:sequence>
<xs:element name="description" type="xs:string"/>
<xs:element name="per-unit-ounces" type="xs:decimal"/>
<xs:element name="price" type="xs:double"/>
<xs:element name="quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipperType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="per-ounce-rate" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

I am just using the default XmlObject.Factory to parse the po.xml file and
then calling the validate method on the resulting XmlObject. On the call to
validate, I get an "Invalid type" error:

Message: Invalid type.
Severity: 0
Line: -1
Location of invalid XML: <purchase-order
xsi:noNamespaceSchemaLocation="po.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
<customer>
...

What does this mean? How can I fix/avoid it? XMLSpy validates both the
schema and xml file without errors.

The document parses without errors, and I can even perform a query against
it using XmlObject.selectPath().

If it helps, here is my code fragment:

File testFile = new File("c:/development/projects/sandbox/po.xml");
System.out.println("File exists: " + testFile.exists() + "; " +
testFile.getAbsolutePath());
XmlObject document = null;
XmlOptions opts = new XmlOptions();
ArrayList errorList = new ArrayList();
opts.setErrorListener(errorList);
try {
document = XmlObject.Factory.parse(testFile, opts);
}
catch (XmlException e) {
System.out.println("exception: " + e.getMessage());
return;
}
catch (IOException e) {
System.out.println("exception: " + e.getMessage());
return;
}

if (errorList.size() != 0) {
System.out.println("parse errors:");

for (int i = 0; i < errorList.size(); i++) {
XmlError error = (XmlError)errorList.get(i);

System.out.println("\n" + "Message: " + error.getMessage());
System.out.println("Severity: " + error.getSeverity());
System.out.println("Line: " + error.getLine());
System.out.println("Location of invalid XML: " +
error.getCursorLocation().xmlText() + "\n");
}

errorList.clear();
}

if (!document.validate(opts)) {
System.out.println("validation errors:");
for (int i = 0; i < errorList.size(); i++) {
XmlError error = (XmlError)errorList.get(i);

System.out.println("\n" + "Message: " + error.getMessage());
System.out.println("Severity: " + error.getSeverity());
System.out.println("Line: " + error.getLine());
System.out.println("Location of invalid XML: " +
error.getCursorLocation().xmlText() + "\n");
}
}

String queryExpression =
"/purchase-order/customer[name='Gladys Kravitz']";

XmlObject[] productObj = document.selectPath(queryExpression);
System.out.println(productObj[0]);

Thanks for any understanding you can share here,
-Mark


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise