首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java: XML到对象,对象到XML和属性

Java: XML到对象,对象到XML和属性
EN

Stack Overflow用户
提问于 2017-07-10 11:48:29
回答 2查看 4.5K关注 0票数 0

我对我目前的项目有个不小的问题,也许你可以帮我.

当我以前处理xml消息时,将转换后的java对象编组成xml,其中java对象属性被设置为xml节点。但是现在我需要将对象属性设置为xml节点属性,如下所示:

代码语言:javascript
复制
<Identification_List counter="">
   <Identification number="XXXXXXX" letter="X" name="PERSON">
      <TravelList counter="">
          <Travel travelType="">
      </TravelList>
   </Identification>
</Identification_List>

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

编辑:示例java类:

代码语言:javascript
复制
public class Identification {
  private int number;
  private char letter;
  private String name;
  private List<Travel> travelList;

  //Add here constructors, getters and setters
}

这个java类应该被封送,其中数字、字母和名称是xml对象属性。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-10 12:56:04

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

代码语言:javascript
复制
@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
}
票数 1
EN

Stack Overflow用户

发布于 2017-07-10 13:19:41

您需要将java转换为xml吗?使用同一个图书馆,

这里有一个例子

1-身份识别类

代码语言:javascript
复制
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-身份识别类

代码语言:javascript
复制
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-旅行者班

代码语言:javascript
复制
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-旅行舱位

代码语言:javascript
复制
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-标识Class类

代码语言:javascript
复制
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-产出:

代码语言:javascript
复制
  <?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45011219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档