我有一个保存为 XML 的对象,读取该 XML,然后保存到另一个 XML。这 2 个 XML 文件是不同的。
第一个 XML(我的预期):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<multiIDTest>
<containerList>
<container id="1">
<elem id="1"/>
</container>
<container container="1" id="2"/>
<container container="2" id="3"/>
</containerList>
</multiIDTest>
第二个 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<multiIDTest>
<containerList>
<container id="1">
<elem id="1"/>
</container>
<container id="2"/>
<container container="2" id="3"/>
</containerList>
</multiIDTest>
如果我更改子元素 ID:
第一个 XML(我的预期):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<multiIDTest>
<containerList>
<container id="1">
<elem id="2"/>
</container>
<container container="1" id="2"/>
<container container="2" id="3"/>
</containerList>
</multiIDTest>
第二个 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<multiIDTest>
<containerList>
<container id="1">
<elem id="2"/>
</container>
<container container="1" id="2"/>
<container container="2" id="3"/>
</containerList>
</multiIDTest>
真奇怪。谁能告诉我为什么?
示例代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package xml;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class MultiIDTest {
static public class Element {
private String id;
@XmlID
@XmlAttribute(required=true)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
static public class Container {
private String id;
@XmlID
@XmlAttribute(required=true)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private Container container;
@XmlIDREF
@XmlAttribute(required=true)
public Container getContainer() {
return container;
}
public void setContainer(Container e) {
this.container = e;
}
private Element elem;
public Element getElem() {
return elem;
}
public void setElem(Element e) {
this.elem = e;
}
}
static public class XmlSerialization {
public static Object read(String filepath, Class... classesToBeBound) {
try {
return JAXBContext.newInstance(classesToBeBound).createUnmarshaller().unmarshal(new File(filepath));
} catch (JAXBException ex) {}
return null;
}
public static void write(String filePath, Object entity, Class... classesToBeBound) {
try {
Marshaller m = JAXBContext.newInstance(classesToBeBound).createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(entity, new File(filePath));
} catch (JAXBException ex) {}
}
}
List<Container> containerList = new ArrayList<>();
@XmlElementWrapper(name="containerList")
@XmlElement(name="container")
public List<Container> getContainerList() {
return (containerList != null)?containerList:(containerList = new ArrayList<>());
}
public void setContainerList(List<Container> containerList) {
this.containerList = containerList;
}
public void initialize() {
Container container = new Container();
container.setId("1");
Element elem = new Element();
elem.setId("2"); // if I use elem.setId("1"), problem occurs.
container.setElem(elem);
containerList.add(container);
Container container2 = new Container();
container2.setId("2");
container2.setContainer(container);
containerList.add(container2);
Container container3 = new Container();
container3.setId("3");
container3.setContainer(container2);
containerList.add(container3);
}
public void write(String filePath, Object obj) {
XmlSerialization.write(filePath, obj, MultiIDTest.class, Container.class, Element.class);
}
public Object read(String filePath) {
return XmlSerialization.read(filePath, MultiIDTest.class, Container.class, Element.class);
}
public static void main(String[] args) {
MultiIDTest test = new MultiIDTest();
String filePath = "c:\\tmp.xml";
test.initialize();
test.write(filePath, test);
MultiIDTest test2 = (MultiIDTest)test.read(filePath);
test.write("c:\\tmp2.xml", test2);
}
}