具有相同名称的XmlAttribute和XmlElement的JAXB XML封送

时间:2019-03-13 10:25:13

标签: java xml jaxb

JAXB似乎无法封送具有带有特定属性的标签的XML,该标签的名称也用于嵌套的子标签(在以下情况下为“ occ”)。

<root>
    <txt occ="1">
        <occ id="1">first element</occ>
        <occ id="2">second element</occ>
    </txt>
    <txt occ="2">
        <occ id="1">first element</occ>
        <occ id="2">second element</occ>
    </txt>
</root>

更改XmlAttribute“ occ”或XmlElement“ occ”的名称即可。

这是我注释Txt类的方式:

@XmlAccessorType(XmlAccessType.FIELD)
public class Txt {

@XmlAttribute(name="occ")
private String occ_attr;

@XmlElement(name="occ")
private List<Occ> occ = new ArrayList<Occ>();

//getters and setters not annotated

这就是我注释Occ类的方式:

public class Occ {

@XmlAttribute
private String id;

@XmlValue
private String value;

//getters and setters not annotated

1 个答案:

答案 0 :(得分:0)

我将它们包裹在一个根中,并且有效。

测试类

import java.io.StringReader;

import javax.xml.bind.*;

public class Test {

    public static void main(String...args) throws JAXBException {
        String text = "<root>\n" + 
                "    <txt occ=\"1\">\n" + 
                "        <occ id=\"1\">first element</occ>\n" + 
                "        <occ id=\"2\">second element</occ>\n" + 
                "    </txt>\n" + 
                "    <txt occ=\"2\">\n" + 
                "        <occ id=\"1\">first element</occ>\n" + 
                "        <occ id=\"2\">second element</occ>\n" + 
                "    </txt>\n" + 
                "</root>";


        JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(text);
        Root root = (Root) unmarshaller.unmarshal(reader);
        System.out.println(root);


        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(root, sw);
        String xmlContent = sw.toString();
        System.out.println(xmlContent);

    }


}

Txt类

import java.util.List;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Txt {

    @XmlAttribute(name = "occ")
    private String occ_attr;

    @XmlElement(name = "occ")
    private ArrayList<Occ> occ;

    public String getOcc_attr() {
        return occ_attr;
    }

    public void setOcc_attr(String occ_attr) {
        this.occ_attr = occ_attr;
    }

    public List<Occ> getOcc() {
        return occ;
    }

    public void setOcc(ArrayList<Occ> occ) {
        this.occ = occ;
    }

    @Override
    public String toString() {
        return "Txt [occ_attr=" + occ_attr + ", occ=" + occ + "]";
    }

}

Occ班

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Occ {

    @XmlAttribute
    private String id;

    @XmlValue
    private String value;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    @Override
    public String toString() {
        return "Occ [id=" + id + ", value=" + value + "]";
    }

}

根类

import java.util.ArrayList;

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

@XmlRootElement(name = "root")
public class Root {
    @XmlElement(name = "txt")
    private ArrayList<Txt> txt;

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

}

打印出:

Root [txt=[Txt [occ_attr=1, occ=[Occ [id=1, value=first element], Occ [id=2, value=second element]]], Txt [occ_attr=2, occ=[Occ [id=1, value=first element], Occ [id=2, value=second element]]]]]

<root>
    <txt occ="1">
        <occ id="1">first element</occ>
        <occ id="2">second element</occ>
    </txt>
    <txt occ="2">
        <occ id="1">first element</occ>
        <occ id="2">second element</occ>
    </txt>
</root>