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

时间:2015-10-18 14:42:36

标签: java xml jaxb

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

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

这是 clinic.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<clinic clinicNumber="1">
    <name>ClinicStackOverFlow</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获得了很好的物理治疗师名单,但我从诊所unmarshaller那里得到的物理治疗师没有得到任何信息:

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

我有点失去它......不知道什么是错的和正确的。在网上尝试了很多解决方案,了解它们中的大部分,但仍然遗漏了一些东西。

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

1 个答案:

答案 0 :(得分:1)

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

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

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

看到您从诊所引用诊所和诊所的PT,我担心即使在那时您也会遇到一个方面的困难。 (我可能错了 - 自从我尝试这个以来已经太久了。)

现在我认为您可能不想合并XML文件。为了解决您的困境,我建议您删除ID和IDREF注释并“手动”设置链接。单次通过PT列表就足够了,这是一个简单而强大的解决方案。

相关问题