Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

java - Binding XML using POJO and JAXB annotations

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>

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

}

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

}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do one of the following options:

OPTION #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

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

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>

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...