JAXB - 无法弄清楚如何正确使用 refID

JAXB - Cannot figure out how to use refID properly

目标:正确编组和解组 clinic.xml

问题:读出物理治疗师(在诊所工作的人)的 ID

这是clinic.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<clinic clinicNumber="1">
    <name>ClinicWhosebug</name>
    <address>Deadbrains</address>
    <zipCode>SomeZip</zipCode>
    <city>City</city>
    <phoneNumber>069441341341</phoneNumber>
    <!-- LIST OF THE ID's of physiotherapists that work here -->
    <physiotherapists>1</physiotherapists>
    <physiotherapists>2</physiotherapists>
</clinic>

Clinic.java

package fysio.shared.domain;
import com.sun.deploy.xml.XMLable;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.List;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Clinic {
    /**
     * The identifier of a clinic
     */
    @XmlID
    @XmlAttribute
    @XmlJavaTypeAdapter(IDStringAdapter.class)
    private String clinicNumber;

    /**
     * The name of a clinic
     */
    private String name;

    /**
     * The address where the clinic is located
     */
    private String address;

    /**
     * The zip code of a clinic
     */
    private String zipCode;

    /**
     * The city a clinic is located in
     */
    private String city;

    /**
     * The phone number of a clinic
     */
    private String phoneNumber;

    @XmlIDREF
    private List<Physiotherapist> physiotherapists;

    /**
     * The default constructor for Jaxb
     */
    public Clinic() {
    }


    public Clinic(String clinicNumber, String name, String address, String zipCode, String city, String phoneNumber, List<Physiotherapist> physiotherapists) {
        this.clinicNumber = clinicNumber;
        this.name = name;
        this.address = address;
        this.zipCode = zipCode;
        this.city = city;
        this.phoneNumber = phoneNumber;
        this.physiotherapists = physiotherapists;
    }

    /**
     * Returns the number of a clinic
     *
     * @return The number of a clinic
     */
    public String getClinicNumber() {
        return clinicNumber;
    }

    /**
     * Sets the number of a clinic
     *
     * @param clinicNumber the number of a clinic
     */
    public void setClinicNumber(String clinicNumber) {
        this.clinicNumber = clinicNumber;
    }


    public List<Physiotherapist> getPhysiotherapists() {
        return physiotherapists;
    }

    /**
     * Sets the physiotherapists of a clinic
     *
     * @param physiotherapists The Physiotherapists of a clinic
     */
    public void setPhysiotherapists(List<Physiotherapist> physiotherapists) {
        this.physiotherapists = physiotherapists;
    }

    /**
     * adds a physiotherapist to a clinic
     *
     * @param physiotherapist The physiotherapist that needs to be added to a clinic
     */
    public void addPhysiotherapist(Physiotherapist physiotherapist) {
        physiotherapists.add(physiotherapist);
    }


}

我们有一份物理治疗师名单(xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<physiotherapists>
    <physiotherapist physiotherapistNumber="1">
        <clinic>1</clinic>
        <name>Henk</name>
    </physiotherapist>
    <physiotherapist physiotherapistNumber="2">
        <clinic>8</clinic>
        <name>Klaas</name>
    </physiotherapist>
</physiotherapists>

Physiotherapist.java(单数)

package fysio.shared.domain;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Physiotherapist {

    @XmlAttribute
    @XmlID
    private String physiotherapistNumber;

    @XmlIDREF
    private Clinic clinic;

    private String name;

    public Physiotherapist() {
        //Default empty constructor for JAXB
    }

    public Physiotherapist(String name, Clinic clinic) {
        this.clinic = clinic;
        this.name = name;
    }

    public Clinic getClinic() {
        return clinic;
    }

    public String getPhysiotherapistNumber() {
        return physiotherapistNumber;
    }

    public void setPhysiotherapistNumber(String physiotherapistNumber) {
        this.physiotherapistNumber = physiotherapistNumber;
   {}
}

Physiotherapists.java(复数)

@XmlRootElement(name = "physiotherapists")
@XmlAccessorType(XmlAccessType.FIELD)
public class Physiotherapists {

    @XmlElement(name = "physiotherapist")
    private List<Physiotherapist> physiotherapistList;

    public Physiotherapists() {
        //empty constructor for xml parsing
        physiotherapistList = new ArrayList<Physiotherapist>();
    }

    public List<Physiotherapist> getPhysiotherapistList() {
        return physiotherapistList;
    }
}

最后是解组部分:

try {
    JAXBContext jc = JAXBContext.newInstance(Clinic.class, Physiotherapist.class, Physiotherapists.class);

    File clinicXML = new File("src/test/resources/data/xml/clinic.data");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Clinic clinicXMLData = (Clinic) unmarshaller.unmarshal(clinicXML);


    File fysiotherapistXML = new File("src/test/resources/data/xml/physiotherapist.data");
    Unmarshaller unmarshaller2 = jc.createUnmarshaller();
    Physiotherapists ph = (Physiotherapists) unmarshaller2.unmarshal(fysiotherapistXML);

} catch (JAXBException e) {
    e.printStackTrace();
}

两位解组员都尽力而为。我从 unmarshaller 2 得到了一份很好的物理治疗师名单,但我没有从 clinic unmarshaller 得到任何关于物理治疗师的信息:

http://imgur.com/Mpcgm8t(堆栈没让我上传图片)

我有点迷路了...不知道什么是错的,什么是对的。试了很多网上的解法,大部分都懂了,但还是有所欠缺。

(这是一个学校项目,还没有重构)

当解组 PT 列表与 Clinic 对象没有任何联系时,如何才能将物理治疗师 (PT) 引用获取到 Clinic 对象中?诊所是根据 XML 数据构建的,其中没有 PT,期间。

要使 XmlID 和 XmlIDREF 正常工作,即要在带注释的 XmlIDREF 字段中存储对象引用,必须有一个合适类型的对象,并且在同一 XML 文件的 XmlID 字段中具有匹配值.

您必须将 XML 数据合并到一个文件中。

看你从PT引用Clinic,从Clinic引用PT,恐怕你到时候也会在一个方向上遇到困难。 (我可能是错的 - 我试过这个已经太久了。)

现在我认为您可能不想合并 XML 文件。为了解决你的困境,我建议你放弃ID和IDREF注释并设置链接"by hand"。通过 PT 列表一次就足够了,这是一个简单而可靠的解决方案。