JAXB (JSR-222)實現對解組時的XML元素順序是相當寬容的。我將在下面舉例說明。
根
下面是有獨立List
性質的域對象。
package forum11519412;
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
List<String> x;
List<Integer> y;
}
的input.xml
下面是一個示例XML文檔,其中List
項目混淆在一起:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<y>1</y>
<x>A</x>
<y>2</y>
<x>B</x>
<y>3</y>
<x>C</x>
</root>
演示
演示代碼將解組input.xml
並將其歸還。
package forum11519412;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11519412/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
輸出
在生成的XML,內容將出現排序。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<x>A</x>
<x>B</x>
<x>C</x>
<y>1</y>
<y>2</y>
<y>3</y>
</root>
你目前得到了哪些類結構?請在這裏描述 – 2012-07-17 09:26:36