JAXB:部分解组返回空域对象 [英] JAXB: partial unmarshalling returns empty domain object

查看:87
本文介绍了JAXB:部分解组返回空域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用部分jaxb unmarshaling技术解组 xml 字符串的<格式良好的部分,在这个问题的答案中描述。

I'm trying to unmarshal a (well-formed) part of a xml string using the "partial jaxb unmarshaling" technique, described in the answer of this question.

为此,我使用了相应XML模式的JAXB域模型(由xjc创建)的子集。该子集完全对应于 XMLStreamReader 处理的部分xml字符串。

For this I'm using a subset of a JAXB domain models (which have been created by xjc) of the corresponding XML schema. This subset corresponds exactly to the part xml string which the XMLStreamReader processes.

确保<$ c $后c> XMLStreamReader 位置是正确的(实际上是第一个元素),我正在调用

After ensuring that the XMLStreamReader position is correct (which is in fact the first element), I'm invoking

 Unmarshaller unmarshaller = JAXBContext.newInstance(jaxbModelClass)
          .createUnmarshaller();

 return unmarshaller.unmarshal(xsr, jaxbModelClass).getValue();

这没有任何异常,返回的对象确实是的一个实例jaxbModelClass ,但不包含任何子元素(全部设置为 null )。

This is done without any exception and the returned object is indeed an instance of jaxbModelClass, however it's not containing any child elements (all are set to null).

这是什么原因?

推荐答案

下面是一个如何处理的例子这个用例。

Below is an example of how you can handle this use case.

下面是我们将使用的XML文档。我们将解组的部分文档从 bar 元素开始。无论是手动创建还是从XML Schema生成类都无关紧要。

Below is the XML document we will use. The partial document we will unmarshal starts at the bar element. It doesn't matter if the class is created by hand or generated from an XML Schema.

<?xml version="1.0" encoding="UTF-8"?>
<foo xmlns="http://www.example.com">
    <bar>
        <baz>Hello World</baz>
    </bar>
</foo>



Java模型



Bar

这是我们要解组的课程。请注意,它没有 @XmlRootElement 注释。

This is the class we will unmarshal. Note that it doesn't have an @XmlRootElement annotation.

package forum22818252;

public class Bar {

    private String baz;

    public String getBaz() {
        return baz;
    }

    public void setBaz(String baz) {
        this.baz = baz;
    }

}

package-info

由于我们的XML文档是名称空间限定的,我们将使用包级别 @XmlSchema 注释(请参阅: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html )。

Since our XML document is namespace qualified we will use the package level @XmlSchema annotation (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html).

@XmlSchema(
    namespace = "http://www.example.com",
    elementFormDefault = XmlNsForm.QUALIFIED)
package forum22818252;

import javax.xml.bind.annotation.*;



演示代码



演示

下面我们将使用StAX XMLStreamReader 解析文档。然后我们将 XMLStreamReader 推进到我们希望解组的本地根。最后因为我们的类没有与之关联的根元素,所以我们将使用一个取消 Class 参数的unmarshal方法。

Below we will parse the document with a StAX XMLStreamReader. Then we advance the XMLStreamReader to the local root we wish to unmarshal. And finally since our class does not have a root element associated with it we will use an unmarshal method that takes a Class parameter.

package forum22818252;

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource source = new StreamSource("src/forum22818252/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        while(!(xsr.isStartElement() && "bar".equals(xsr.getLocalName()))) {
            xsr.next();
        }

        JAXBContext jc = JAXBContext.newInstance(Bar.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Bar bar = unmarshaller.unmarshal(xsr, Bar.class).getValue();

        System.out.println(bar.getBaz());
    }

}

输出

以下是运行演示代码的输出。

Below is the output from running the demo code.

Hello World

这篇关于JAXB:部分解组返回空域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆