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

JochemQuery

目标:正确编组和取消编排clinic.xml

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

这是clipart.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(堆栈没有让我上传照片)

我有点失去了...不再知道什么是错误的和正确的。在线尝试了许多解决方案,了解了大多数解决方案,但仍漏了一些东西。

(这是一个学校项目,尚未重构)

异想天开

当解组PT列表与那些诊所对象没有任何联系时,如何将物理治疗师(PT)引用纳入诊所对象?诊所是根据XML数据建立的,在此期间没有PT。

为了使XmlID和XmlIDREF起作用,即,要将对象引用存储在带有注释的XmlIDREF的字段中,必须在同一XML文件中具有适当类型的对象,并且在其XmlID字段中具有匹配的值。

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

看到您引用了PT的诊所和诊所的PT,恐怕您甚至会在一个方向上遇到困难。(我可能是错的-自从我尝试过以来太久了。)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法弄清楚如何正确调用元素

来自分类Dev

我无法弄清楚如何在我的应用程序中正确使用notifyDataSetChanged

来自分类Dev

无法弄清楚如何使用此MKLINK(是的,我之前已经正确阅读了文档)

来自分类Dev

无法弄清楚如何使用Soundcloud API测试代理

来自分类Dev

无法弄清楚如何使用ImageViewTouch挂钩

来自分类Dev

无法弄清楚如何使用getchar(); 在C

来自分类Dev

无法弄清楚如何使用getchar(); 在C中

来自分类Dev

无法弄清楚如何为我的函数使用参数

来自分类Dev

无法弄清楚如何使用$ scope。$ watch

来自分类Dev

无法弄清楚如何使用西班牙口音

来自分类Dev

无法弄清楚如何使用lighttpd接收HTTP请求

来自分类Dev

无法弄清楚如何使用 OfficeExtension.Promise

来自分类Dev

无法弄清楚如何<s:select>

来自分类Dev

无法弄清楚如何收税

来自分类Dev

无法弄清楚如何捕获InputMismatchException

来自分类Dev

无法弄清楚如何打印toString

来自分类Dev

iOS限制-无法弄清楚如何

来自分类Dev

使用css3关键帧滑动效果,但无法弄清楚如何正确配置。

来自分类Dev

无法弄清楚如何正确输入我的N元树

来自分类Dev

无法弄清楚如何在 augeas 中正确添加 onlyif

来自分类Dev

无法弄清楚如何正确存储命令行参数

来自分类Dev

无法弄清楚用法

来自分类Dev

无法弄清楚崩溃

来自分类Dev

如何使用CXF正确设置JAXB批注?

来自分类Dev

如何弄清楚放入CSS的正确目标是什么

来自分类Dev

试图弄清楚如何正确打破这些嵌套循环

来自分类Dev

如何命名JAXB类?

来自分类Dev

如何用jaxb编组?

来自分类Dev

JAXB如何推进XMLStreamReader?