Java:XML to Object和Object to XML with properties

时间:2017-07-10 11:48:30

标签: java xml properties jaxb

我目前的项目有一个不小的问题,可能你可以帮我解决这个问题......

当我之前使用xml消息时,将转换后的java对象编组为xml,其中java对象属性设置为xml节点。但现在我需要将该对象属性设置为xml节点属性,如下所示:

<Identification_List counter="">
   <Identification number="XXXXXXX" letter="X" name="PERSON">
      <TravelList counter="">
          <Travel travelType="">
      </TravelList>
   </Identification>
</Identification_List>

我该怎么做?我应该使用什么框架?谢谢!

编辑:示例java类:

public class Identification {
  private int number;
  private char letter;
  private String name;
  private List<Travel> travelList;

  //Add here constructors, getters and setters
}

那个java类应该被编组,其中数字,字母和名称是xml对象属性

2 个答案:

答案 0 :(得分:1)

你需要将java转换为xml吗?使用相同的库,

这是一个例子

1-IdentificationList Class

package test;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;


@XmlRootElement(name = "Identification_List")

public class IdentificationList {

    private int counter;
    private List<Identification> identificationList;

    public IdentificationList() {
    }

    public IdentificationList(List<Identification> identificationList) {
        this.identificationList = identificationList;
        this.counter = identificationList == null ? 0 : identificationList.size();
        ;
    }

    @XmlElement(name = "Identification")
    public List<Identification> getIdentificationList() {
        return identificationList;
    }

    public void setIdentificationList(List<Identification> identificationList) {
        this.identificationList = identificationList;
    }

    @XmlAttribute
    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

2-Identification Class

package test;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;






@XmlType(propOrder = {"number", "letter", "name","travelList"})
public class Identification {


    private int number;

    private String letter;

    private String name;

    private TravelList travelList;


    public Identification() {
    }

    public Identification(int number, String letter, String name, TravelList travelList) {
        this.number = number;
        this.letter = letter;
        this.name = name;
        this.travelList = travelList;
    }
    @XmlAttribute
    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
     @XmlAttribute
    public String getLetter() {
        return letter;
    }

    public void setLetter(String letter) {
        this.letter = letter;
    }
     @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @XmlElement(name = "TravelList")
    public TravelList getTravelList() {
        return travelList;
    }

    public void setTravelList(TravelList travelList) {
        this.travelList = travelList;
    }
}

3-TravelList Class

package test;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.util.List;


public class TravelList {


    private List<Travel>travels;
    private int counter;

    public TravelList() {
    }

    public TravelList(List<Travel> travels) {
        this.travels = travels;
        this.counter=travels==null?0:travels.size();
    }
    @XmlElement(name = "Travel")
    public List<Travel> getTravels() {
        return travels;
    }

    public void setTravels(List<Travel> travels) {
        this.travels = travels;
    }
    @XmlAttribute
    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

4-Travel Class

package test;

import javax.xml.bind.annotation.XmlAttribute;


public class Travel {

    private String travelType;

    public Travel() {
    }

    public Travel(String travelType) {
        this.travelType = travelType;
    }
    @XmlAttribute
    public String getTravelType() {
        return travelType;
    }

    public void setTravelType(String travelType) {
        this.travelType = travelType;
    }
}

5-IdentificationToXML Class

package test;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.FileOutputStream;
import java.util.ArrayList;


public class IdentificationToXML {


    public static void main(String ...args) throws  Exception {


        JAXBContext contextObj = JAXBContext.newInstance(IdentificationList.class);

        Marshaller marshallerObj = contextObj.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Travel travel1=new Travel("My First Travel");
        Travel travel2=new Travel("My Second Travel");


        Travel travel3=new Travel("My Third Travel");
        Travel travel4=new Travel("My Fourth Travel");


        ArrayList<Travel> list=new ArrayList<Travel>();
        list.add(travel1);
        list.add(travel2);



        ArrayList<Travel> list2=new ArrayList<Travel>();
        list2.add(travel3);
        list2.add(travel4);

        Identification identification1=new Identification(111,"c","My Name",new TravelList(list));
        Identification identification2=new Identification(222,"d","My Name",new TravelList(list2));
        ArrayList<Identification> list3=new ArrayList<Identification>();
        list3.add(identification1);
        list3.add(identification2);
        IdentificationList identification=new IdentificationList(list3);

        marshallerObj.marshal(identification, new FileOutputStream("identification.xml"));
    }
}

6-Output:

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Identification_List counter="2">
        <Identification number="111" letter="c" name="My Name">
            <TravelList counter="2">
                <Travel travelType="My First Travel"/>
                <Travel travelType="My Second Travel"/>
            </TravelList>
        </Identification>
        <Identification number="222" letter="d" name="My Name">
            <TravelList counter="2">
                <Travel travelType="My Third Travel"/>
                <Travel travelType="My Fourth Travel"/>
            </TravelList>
        </Identification>
    </Identification_List>

答案 1 :(得分:0)

您需要向类中添加适当的JAXB注释,以告诉JAXB如何将类映射到XML。例如:

@XmlRootElement(name = "Identification_List")
@XmlAccessorType(XmlAccessType.FIELD)
public class IdentificationList {

    @XmlAttribute
    private int counter;

    @XmlElement(name = "Identification")
    private List<Identification> identifications;

    // getters and setters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Identification {

    @XmlAttribute
    private int number;

    @XmlAttribute
    private char letter;

    @XmlAttribute
    private String name;

    @XmlElementWrapper(name = "TravelList")
    @XmlElement(name = "Travel")
    private List<Travel> travels;

    // getters and setters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class Travel {

    @XmlAttribute
    private String travelType;

    // getters and setters
}