如何在Java中使用对象属性作为标记名称进行编组和解组?

时间:2018-01-29 10:30:35

标签: java jaxb marshalling unmarshalling

我有一个类似下面的课程

class Element

{

   private String sElementName;

   private String sElementValue;

   private HashMap<String, String> hmAttrMap;

   private ArrayList<Element> alChildren;

}

考虑上述类的对象如下:

  MyElement e1 = new MyElement();

    e1.sElementName = "ElementName1";

    e1.sElementValue = "";



    HashMap<String, String> hm = new HashMap<>();

    hm.put("att1", "value1");

    hm.put("att2", "value2");

    e1.hmAttrMap = hm;



    MyElement e2 = new MyElement();

    e2.sElementName = "ElementName2";

    e2.sElementValue = "ElementValue2";



    ArrayList<MyElement> al = new ArrayList<>();

    al.add(e2);



    e1.alChildren = al;

在编组对象e1

后,我需要以下XML
<ElementName1>

<ElementName2   att1=’value1’   att2=’value2’>ElementValue2</ElementName2>

</ElementName1>

是否可以使用JAXB这样做,我正在努力将XML标记的名称作为对象属性而不是类名。另外,如果有办法使用JAXB,它也可以解组吗?

1 个答案:

答案 0 :(得分:0)

Element类:您将ElementName1作为根元素,将ElementName2作为子节点,将每个作为JaxB中的一个单独考虑我们可以实现此目的。

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ElementName1")
public class Element {

    @XmlElement(name = "ElementName2")
    Elem ele;

    public Element() {

    }

    public Elem getEle() {
        return ele;
    }

    public void setEle(Elem ele) {
        this.ele = ele;
    }

    @Override
    public String toString() {
        return "Element[  ele=" + ele + "]";
    }
}

元素clss2

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="ElementName2")
public class Elem {

   @XmlAttribute(name="att1")
   private int attr1;
   @XmlAttribute(name="att2")
   private int attr2;

    public Elem() {
        // TODO Auto-generated constructor stub
    }
    public int getAttr1() {
        return attr1;
    }
    public void setAttr1(int attr1) {
        this.attr1 = attr1;
    }
    public int getAttr2() {
        return attr2;
    }
    public void setAttr2(int attr2) {
        this.attr2 = attr2;
    }

    @Override
    public String toString() {
        return "Elem[ attr1=" + attr1 + ",attr2="+attr2+"]";
    }

}

在运行时赋予属性值的主类

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;


public class Runningclass {

    public static void main(String[] args) {
        Elem  ele2=new Elem();
        ele2.setAttr1(2);
        ele2.setAttr2(4);
        Element ele= new Element();
        ele.setEle(ele2);

        JAXBContext jaxbContext;
        try {
            jaxbContext = JAXBContext.newInstance(Element.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            /* set this flag to true to format the output */
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            /* marshaling of java objects in xml (output to file and standard output) */
            jaxbMarshaller.marshal(ele, new File("country.xml"));
            jaxbMarshaller.marshal(ele, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

和输出:country.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ElementName1>
    <ElementName2 att1="2" att2="4"/>
</ElementName1>