@XmlValue
可以应用于List
属性。使用此映射,列表项将在 XML 中表示为空格分隔的列表。您可以执行以下操作:
元素
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Element {
@XmlAttribute
private String attribute1;
@XmlAttribute
private String attribute2;
@XmlAttribute
private String attribute3;
@XmlValue
private List<String> value;
}
演示
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Element.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17775900/input.xml");
Element element = (Element) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(element, System.out);
}
}
输入.xml/输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<element attribute1="a" attribute2="b" attribute3="c">a b c d e f g</element>