java 使用 POJO 和 JAXB 注释绑定 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11311374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:37:45  来源:igfitidea点击:

Binding XML using POJO and JAXB annotations

javajaxbpojo

提问by Alex Dowining

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:

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

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

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

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

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

  @XmlElement
  private List<String> data;

  //get/set methods

}

And also i try and this POJO:

我也尝试使用这个 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

}

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?

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

回答by bdoughan

You can do one of the following options:

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

OPTION #1

选项1

Datas

数据

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

}

For More Information

想要查询更多的信息



OPTION #2

选项#2

Datas

数据

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

}

Data

数据

package forum11311374;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Data{

  @XmlValue
  private String data;

  //get/set methods

}

For More Information

想要查询更多的信息



The following can be used with both options:

以下内容可与两个选项一起使用:

input.xml/Ouput

输入.xml/输出

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

我已更新 XML 文档以包含必要的结束标记。 <data>apple</data>而不是<data>apple<data>.

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

Demo

演示

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

}

回答by josseyj

The first option did work for me... not sure why you are getting the problem... Try this annotation...

第一个选项对我有用......不知道为什么你会遇到这个问题......试试这个注释......

@XmlElements(@XmlElement(name="data", type=String.class))
private List<String> datas; //ignore the variable name