如何使用JAXB解组重复的嵌套类? [英] How to unmarshall repeated nested classes with JAXB?

查看:107
本文介绍了如何使用JAXB解组重复的嵌套类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何指示JAXB处理此事?

How can I instruct JAXB to process this ?

XML

<root>
 <parent>
    <child id="1" name="foo" />
 </parent>
 <parent>
    <child id="3" name="foo2" />
 </parent>
 <parent>
    <child id="4" name="bar2" />
 </parent>
 <parent>
    <child id="2" name="bar" />
 </parent>
</root>

Root.java

@XmlRootElement
public class Root {
   @XmlElement(name="parent/child")
   List<Child> allChildren;
}

这不起作用... allChildren为空。

This doesn't work ... allChildren is empty.

推荐答案

您可以更改模型并执行以下操作:

You could change your model and do the following:

Root

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
   @XmlElement(name="parent")
   List<Parent> allParents;
}

父母

@XmlAccessorType(XmlAccessType.FIELD)
public class Parent {
   @XmlElement(name="child")
   List<Child> allChildren;
}






UPDATE


是否可以避开父类?

Is it possible to avoid the parent class ?

有几种不同的方法可以实现这一目标:

There are a couple of different ways to accomplish this:

选项#1 - 使用XmlAdapter的任何JAXB实现

您可以使用XmlAdapter虚拟添加 Parent 类。

You could use an XmlAdapter to virtually add in the Parent class.

ChildAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class ChildAdapter extends XmlAdapter<ChildAdapter.Parent, Child> {

    public static class Parent {
        public Child child;
    }

    @Override
    public Parent marshal(Child v) throws Exception {
        Parent parent = new Parent();
        parent.child = v;
        return parent;
    }

    @Override
    public Child unmarshal(Parent v) throws Exception {
        return v.child;
    }

}

Root

@XmlJavaTypeAdapter 注释用于引用 XmlAdapter

import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

   @XmlElement(name="parent")
   @XmlJavaTypeAdapter(ChildAdapter.class)
   List<Child> allChildren;

}

儿童

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Child {

    @XmlAttribute
    int id;

    @XmlAttribute
    String name;

}

选项#2 - 使用EclipseLink JAXB(MOXy )

如果您使用的是 EclipseLink JAXB(MOXy) 作为您的 JAXB(JSR-222) 实施然后你可以做以下事情(注意:我是MOXy领导):

If you are using EclipseLink JAXB (MOXy) as your JAXB (JSR-222) implementation then you could do the following (Note: I'm the MOXy lead):

Root

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

   @XmlElement(name="parent")
   List<Child> allChildren;

}

儿童

MOXy的 @XmlPath 注释与您尝试使用 @XmlElement 帖子中的注释。

MOXy's @XmlPath annotation works pretty much the way you are trying to use the @XmlElement annotation in your post.

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlAccessorType(XmlAccessType.FIELD)
public class Child {

    @XmlPath("child/@id")
    int id;

    @XmlPath("child/@name")
    String name;

}

更多信息

  • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

这篇关于如何使用JAXB解组重复的嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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