General namespace for mapped class

3

Can anyone help me with this question?

Based on a result XML from a WebService (OBIEE) I generated an XSD in a online converter and with this XSD I generated the class mapped by means of the command xjc .

When I tried to play the XML result 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 due to the presence of the xmlns attribute in the first line of XML.

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

I ended up solving this error by putting in all the annotations @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? Is there a way to define this namespace in a more general way?

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>

Generated XSD

<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>
    
asked by anonymous 06.04.2015 / 22:18

1 answer

4

At first glance, it looks like you're looking for the annotation @XmlSchema . With this annotation you can map namespaces, prefixes, etc., even at packet level (thicker granularity).

To use this type of strategy you should create a package-info.java file within the desired package and write it down as needed.

@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 xfc continuously (eg, updating the service and re-generating the classes), it might be worth playing around with the XSD framework, JAXB Bindings , etc; even more so in the case where XSD was automatically generated. It's even good to check if you really need the namespace to conform to the service.

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

Source: Java XML and JSON Binding

    
07.04.2015 / 01:44