You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Mark Womack <wo...@adobe.com> on 2005/01/07 00:39:25 UTC

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


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


RE: invalid type error?

Posted by Mark Womack <wo...@adobe.com>.
Playing with this further, if I generate java classes for the schema (using
scomp), then the document is validated without errors.

So, I am guessing this means that validation via the generic XmlObject is
not supported even though the xml document specifies a schema?  Validation
is only supported in the generated classes?

Thanks,
-Mark

> -----Original Message-----
> From: Mark Womack [mailto:womack@adobe.com]
> Sent: Thursday, January 06, 2005 3:39 PM
> To: user@xmlbeans.apache.org
> 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
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org