栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用JAXB解组重复的嵌套类?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用JAXB解组重复的嵌套类?

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

@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;}

更新

有可能避免上课吗?

有两种不同的方法可以完成此操作:

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

您可以使用XmlAdapter虚拟添加

Parent
类。

子适配器

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;    }}

@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主管):

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
在帖子中使用注释的方式一样。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454424.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号