使用POJO和JAXB注释绑定XML [英] Binding XML using POJO and JAXB annotations

查看:136
本文介绍了使用POJO和JAXB注释绑定XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml格式,我想通过POJO绑定它并使用JAXB注释。 XML格式如下:

I have the following xml format that i want to bind it through a POJO and using JAXB annotations. The XML format is the following:

 <datas>
   <data>apple<data>
   <data>banana<data>
   <data>orange<data>
 <datas>

我正在尝试通过以下POJO绑定数据:

And i'm trying to bind the data through the following POJO:

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

  @XmlElement
  private List<String> data;

  //get/set methods

}

我也尝试这个POJO:

And also i try and this POJO:

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

  @XmlElement
  private List<Data> datas;

  //get/set methods

}

//

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

  @XmlElement
  private String data;

  //get/set methods

}

在第一种情况下,它只检索第一个数据:apple。在第二种情况下不检索任何东西。有人可以帮我提供适当的POJO和注释以绑定所有数据吗?

In the first case it retrieves only the first data: apple. In the second case doesn't retrieve anything. Could someone help me to provide the appropriate POJO and annotations in order to bind all data?

推荐答案

您可以执行以下操作之一选项:

You can do one of the following options:

选项#1

数据

package forum11311374;

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

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

  private List<String> data;

  //get/set methods

}

更多信息

  • http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html

选项#2

数据

package forum11311374;

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

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

  @XmlElement(name="data")
  private List<Data> datas;

  //get/set methods

}

数据

package forum11311374;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Data{

  @XmlValue
  private String data;

  //get/set methods

}

更多信息

  • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html

以下两个选项均可使用以下内容:

The following can be used with both options:

input.xml /输出

我已更新XML文档以包含必要的内容关闭标签。 < data> apple< / data> 而不是< data> apple< data>

I have updated the XML document to contain the necessary closing tags. <data>apple</data> instead of <data>apple<data>.

<datas>
   <data>apple</data>
   <data>banana</data>
   <data>orange</data>
 </datas>

演示

package forum11311374;

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11311374/input.xml");
        Datas datas = (Datas) unmarshaller.unmarshal(xml);

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

}

这篇关于使用POJO和JAXB注释绑定XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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