如何在 XmlElements 列表中获取 XmlElement 名称 [英] How to get XmlElement name in list of XmlElements

查看:25
本文介绍了如何在 XmlElements 列表中获取 XmlElement 名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个 REST 服务获取 XML,如下所示:

I am getting XML from a REST service that looks like:

<entity>
  <foo>
    <count>1</count>
    <date>1970-01-01</date>
    <margin>78.67</margin>
  </foo>
  <bar>
    <count>2</count>
    <date>1450-09-17</date>
    <margin>24.56</margin>
  </bar>
  <baz>
    <count>11</count>
    <date>1968-11-12</date>
    <margin>98.76</margin>
  </baz>
</entity>

我正在解析一个具有以下内容的类 Entity.java:

And I'm parsing with a class Entity.java that has:

@XmlRootElement(name = "entity")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entity implements Serializable {

  @XmlElements({
        @XmlElement(name="foo"),
        @XmlElement(name="bar"),
        @XmlElement(name="baz")
  })
  private List<EntityElement> entityElements;
....

使用 EntityElement 类,例如:

With an EntityElement class like:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EntityElement implements Serializable {
    @XmlElement(required = true)
    private int count

    @XmlElement(required = true)
    private String date;

    @XmlElement(required = false)
    private long margin;
....

这一切都按照我的预期工作,我得到了一个实体元素列表.我想要做的是添加一个名称字段,并用找到的元素的名称(foo、bar 或 baz)填充它,但我没有看到任何方法可以做到这一点.任何帮助或建议将不胜感激.

This all works exactly like I expect and I get a list of EntityElements. What I would like to do is add a name field and have it populated with the name of the element that was found (a foo, bar, or baz) but I'm not seeing any way to do this. Any help or suggestions would be appreciated.

推荐答案

注意:我是EclipseLink JAXB (MOXy) 领导和成员 JAXB (JSR-222) 专家组.

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

对于此用例,您可以使用我们在 EclipseLink 2.5.1 中添加到 MOXy 的 @XmlVariableNode.

You could use the @XmlVariableNode that we added to MOXy in EclipseLink 2.5.1 for this use case.

实体

我们将在 entityElements 字段上使用 MOXy 的 @XmlVariableNode.在字段上,我们指定目标类上表示元素名称的字段.

We will use MOXy's @XmlVariableNode on the entityElements field. On the field we specify the field on the target class that represents the element name.

import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlVariableNode;

@XmlRootElement(name = "entity")
@XmlAccessorType(XmlAccessType.FIELD)
public class Entity implements Serializable {

    @XmlVariableNode("name")
    private List<EntityElement> entityElements;

}

实体元素

我们将使用 @XmlTransient 注释来防止 name 字段的值被编组为子元素.

We will use the @XmlTransient annotation to prevent the value of the name field from being marshalled as a child element.

import java.io.Serializable;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class EntityElement implements Serializable {

    @XmlTransient
    private String name;

    @XmlElement(required = true)
    private int count;

    @XmlElement(required = true)
    private String date;

    @XmlElement(required = false)
    private long margin;

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要在与域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示代码

演示

由于 MOXy 是 JAXB (JSR-222) 实现,因此只需要标准运行时 API.

Since MOXy is a JAXB (JSR-222) implementation only the standard runtime APIs are required.

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Entity.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum19194567/input.xml");
        Entity entity = (Entity) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(entity, System.out);
    }

}

input.xml/输出

这是您问题中的 XML 文档.

This is the XML document from your question.

<?xml version="1.0" encoding="UTF-8"?>
<entity>
   <foo>
      <count>1</count>
      <date>1970-01-01</date>
      <margin>0</margin>
   </foo>
   <bar>
      <count>2</count>
      <date>1450-09-17</date>
      <margin>0</margin>
   </bar>
   <baz>
      <count>11</count>
      <date>1968-11-12</date>
      <margin>0</margin>
   </baz>
</entity>

了解更多信息

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