如何使用JAXB删除XML中的标记 [英] How to remove the tag in XML using JAXB

查看:410
本文介绍了如何使用JAXB删除XML中的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB将java对象转换为xml文件。

I'm using JAXB to convert java object into xml file.

在我的XML文件中,我需要删除标签 使用XSLT。

In my XML file, I need to remove the tag without using XSLT .

例如:删除标签订单

<order_List>
  <orders>
    <orderid>12324<orderid>
  </orders>
</order_List>

例外结果:

<order_List>
   <orderid>12324<orderid>
</order_List>


推荐答案

我可以建议你天真的做法。

I can suggest you "naive" approach.

可以使用JAXB注释配置包装器标记 orders @XmlElementWrapper 。因此,您可以创建2个模型:一个包含此标记,另一个不包含此标记。您可以使用包含此标记的模型来解析数据,然后将数据复制到不包含此标记的模型,然后使用它进行序列化。

The wrapper tag orders can be configured using JAXB annotation @XmlElementWrapper. So, you can create 2 models: one that contain this tag, another that does not. You can use the model that contains this tag to parse you data, then copy data to model that does not contain this tag and then use it to serialize.

@XmlRootElement(name = "index-annotations")
public class OrderList {
   private Collection<Integer> orderIds;

   @XmlElement(name = "orderid", type = Integer.class)
   public Collection<Integer> getOrderId() {
       return orderIds;
   }
}


@XmlRootElement(name = "index-annotations")
public class OutputOrderList extends OrderList {
   @Override
   @XmlElement(name = "orderid", type = Integer.class)
   @XmlElementWrapper(name="orders")
   public Collection<Integer> getOrderId() {
       return orderIds;
   }
}

显然这个解决方案包含一种重复的代码,但是由于注释有效性的编译时验证,因此使用XML配置2个模式可能更好。

Obviously this solution contains a kind of duplicate code, however it is probably better then configuring 2 schemas using XML because of compile time validation of annotations validity.

这篇关于如何使用JAXB删除XML中的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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