简体   繁体   English

Java:具有属性的XML到对象和XML到对象

[英]Java: XML to Object and Object to XML with properties

I have a not-little problem with my current project, and probably you can help me with this... 我当前的项目有一个小问题,也许您可​​以在此方面帮助我...

When I worked before with xml messages, marshalling converted java objects to xml, where the java object attributes were set to xml nodes. 当我以前处理xml消息时,将转换后的java对象编组为xml,其中java对象属性设置为xml节点。 But now I need to set that object attributes to xml node attributes, as I show here: 但是现在我需要将该对象属性设置为xml节点属性,如下所示:

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

How can I do this? 我怎样才能做到这一点? What framework should I use? 我应该使用什么框架? Thank you! 谢谢!

Edit: Example java class: 编辑:示例java类:

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

  //Add here constructors, getters and setters
}

That java class is the one that should be marshalled, where number, letter and name are the xml object properties 该Java类是应编组的类,其中数字,字母和名称是xml对象的属性

Do you need to convert java to xml ? 您需要将java转换为xml吗? use the same library , 使用相同的库,

Here an Example 这是一个例子

1-IdentificationList Class 1-IdentificationList类

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 2-识别等级

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 3-TravelList类别

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 4次旅行

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 5-IdentificationToXML类

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 : 6路输出

  <?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>

You need to add the appropriate JAXB annotations to your classes, to tell JAXB how to map your class to XML. 您需要在类中添加适当的JAXB批注,以告诉JAXB如何将您的类映射到XML。 For example: 例如:

@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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM