繁体   English   中英

JAXB返回JAXBElement而不是root元素

[英]JAXB returning a JAXBElement instead of root element

通常JAXB可以正常工作,但它不能正确地解析我的模式,或者因为它应该将XMLRootElement放在我的对象上而不是。 我使用这段代码来解组​​失败,我不得不求助于将包用作String而不是..

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

这是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>

最后,我的结果对象缺少XmlRootElement。 有什么想法发生了什么?

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ProductsDomainResponseType", propOrder = {
    "invocationOutcome",
    "products"
})
public class ProductsDomainResponseType {

我现在正在处理的项目中遇到了这个问题。 尝试以下内容。 具体来说,最后几行应该是有帮助的。

        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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM