1

我有一个保存为 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);
    }
}
4

1 回答 1

1

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

JAXB RI 问题

JAXB 2.2 (JSR-222) 可以处理这个用例,但是在您使用的特定 JAXB 实现中碰巧有一个错误阻止它工作。我已经能够确认您在使用 JDK 1.7.0 for Mac 中包含的 JAXB 2.2 实现时看到的问题。我建议为此问题打开一个错误:

其他 JAXB 实现

我已经使用 EclipseLink JAXB (MOXy) 尝试了您的示例代码,它按预期运行。我是 MOXy 负责人,您可以从以下位置下载它。

了解更多信息

于 2012-10-10T13:13:53.420 回答