2

Usually JAXB works but it is not parsing my schema correctly or something as it should be putting XMLRootElement on my object and is not. I use this code to ry to unmarshal which fails and I have to resort to using the package as a String instead..

ProductsDomainResponseType resp = unmarshaller.unmarshall(ProductsDomainResponseType.class, strXmlInput);

This is the xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.cigna.com/acme/services/product/2012/03"
    xmlns:prd="http://www.cigna.com/IFPRetail/Product/0.1" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:outc="http://www.cigna.com/acme/domains/utility/outcome/2010/03"
    targetNamespace="http://www.cigna.com/acme/services/product/2012/03"
    elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:import namespace="http://www.cigna.com/IFPRetail/Product/0.1" schemaLocation="product.xsd" />
    <xs:import namespace="http://www.cigna.com/acme/domains/utility/outcome/2010/03" schemaLocation="InvocationOutcome_2010_03.xsd" />

    <xs:element name="productsDomainResponse" type="ProductsDomainResponseType">
        <xs:annotation>
            <xs:documentation>in case of error use invocationOutcome element from
                InvocationOutcome XSD in utility namespace</xs:documentation>
        </xs:annotation>
    </xs:element>

    <xs:complexType name="ProductsDomainResponseType">
        <xs:annotation>
            <xs:documentation>Products Domain XML Response</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="outc:invocationOutcome" />
            <xs:element name="products" type="ProductType" minOccurs="0" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="ProductType">
        <xs:sequence>
            <xs:element ref="prd:product" />

        </xs:sequence>
    </xs:complexType>

</xs:schema>

Finally, my resulting object is missing the XmlRootElement. Any ideas what is going on?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ProductsDomainResponseType", propOrder = {
    "invocationOutcome",
    "products"
})
public class ProductsDomainResponseType {
skaffman
  • 398,947
  • 96
  • 818
  • 769
Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • 1
    Possible duplicate of http://stackoverflow.com/questions/819720/no-xmlrootelement-generated-by-jaxb – skaffman Mar 20 '12 at 17:35
  • 1
    sweet, that post had the exact response to my question(I did not find that in my searches and I searched for quite some time on the issue) – Dean Hiller Mar 22 '12 at 17:30

1 Answers1

4

I had this problem on the project I'm working on now. Give the following a try. Specifically the last few lines should be helpful.

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream is = post.getResponseBodyAsStream();
        Document doc = db.parse(is);
        DOMSource ds = new DOMSource(doc);
        JAXBContext responseContext = JAXBContext.newInstance(SearchEnrolledFundingResponse.class);
        Unmarshaller u = responseContext.createUnmarshaller();
        JAXBElement<SearchEnrolledFundingResponse> jaxbResponse = (JAXBElement<SearchEnrolledFundingResponse>) u.unmarshal(ds, SearchEnrolledFundingResponse.class);
        SearchEnrolledFundingResponse searchResponse = jaxbResponse.getValue();
derdc
  • 1,081
  • 2
  • 7
  • 19