General namespace for mapped class

Asked

Viewed 275 times

3

Could someone help me with that question?

Based on a Webservice result XML (OBIEE) I generated an XSD in a online converter and with this XSD I managed the class mapped through the command xjc.

When I tried to play the result in XML in the generated class (via JAXB unmarshaller) the following error occurred:

javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:schemas-microsoft-com:xml-analysis:rowset", local:"rowset"). Expected elements are <{}rowset>

This error was generated by the presence of the attribute xmlns in the first line of XML.

<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset" >

I ended up solving this mistake by putting in all the notes @XmlElement and @XmlType the attribute namespace = "urn:schemas-microsoft-com:xml-analysis:rowset" ex:

@XmlRootElement(name = "rowset",  namespace = "urn:schemas-microsoft-com:xml-analysis:rowset" )

My question is: how to solve the problem without having to play namespaces in all these notes? There is a way to define this namespace more generally?

XML

<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset" >
<Row>
    <Column0>1337.0</Column0>
    <Column1>TESTE1</Column1>
    <Column2>2015-02-01T00:00:00</Column2>
    <Column3>47367.0</Column3>
    <Column4>129598.0</Column4>
    <Column5>142231.0</Column5>
</Row>
<Row>
    <Column0>1337.0</Column0>
    <Column1>TESTE2</Column1>
    <Column2>2015-03-01T00:00:00</Column2>
    <Column3>224892.0</Column3>
    <Column4>1624674.0</Column4>
    <Column5>1289782.0</Column5>
</Row>
</rowset>

XSD Generated

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="rowset">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Row" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element type="xs:float" name="Column0"/>
                        <xs:element type="xs:string" name="Column1"/>
                        <xs:element type="xs:dateTime" name="Column2"/>
                        <xs:element type="xs:float" name="Column3"/>
                        <xs:element type="xs:float" name="Column4"/>
                        <xs:element type="xs:float" name="Column5"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

1 answer

4


At first glance it seems that you are searching for the note @Xmlschema. With this annotation you can map namespaces, prefixes, etc, including at the packet level (coarser granularity).

To make use of this type of strategy you must create a file package-info.java within the desired package and write it down according to your needs.

@javax.xml.bind.annotation.XmlSchema (
    namespace = "urn:schemas-microsoft-com:xml-analysis:rowset", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)  
package meu.pacote;

If you intend to use the xfc continuously (e.g., keep updating the service and generating classes again), it might be worth playing a little with the structure of XSD, JAXB Bindings, etc; especially in this case where XSD was generated automatically. It’s also good to check if you really need the namespace to conform to the service.

I’ve had to maintain software with super complex chains of jaxb bindings (beyond infinite tasks ant) to clean namespaces, include prefixes, delete duplicate classes, etc... This usually becomes a mess. Often the problem can be solved much more cleanly by simplifying the Xsds (and eventually the payload expected), even services out of your control are often far more flexible and lenient than we expect.


Source: Java XML and JSON Binding

  • Thank you very much, Anthony. !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.