java - JAXB 返回 JAXBElement 而不是根元素

标签 java jaxb

通常 JAXB 可以工作,但它无法正确解析我的架构,或者它应该将 XMLRootElement 放在我的对象上,但事实并非如此。我使用这段代码来解码​​,但失败了,我不得不使用包作为字符串来代替..

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

关于java - JAXB 返回 JAXBElement 而不是根元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9791992/

相关文章:

java - 如何远程调试我的 rcp 应用程序?

java - Android 搜索栏值更改按钮

java - 将生成的源作为源文件夹添加到 Eclipse

java - swing,在可编辑的 jTable 上显示 JList 以选择条目以自动完成文本

java邮件程序不再发送邮件

java - 一场 war 在 Tomcat 中呼唤另一场 war

java - JAX-B 嵌套列表

java - 将 Moxy 设置为默认 jaxb 实现提供者

xml - 接口(interface) EJB - 使用 JAXB 接口(interface)的 XML

java - 明确表示不识别 JAXBElement