gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:02:59 25 4
gpt4 key购买 nike

目标:正确编码和解码 Clinic.xml

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

这是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>

Physiotherapy.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;
{}
}

Physiotherapys.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 获得任何有关物理治疗师的信息:

/image/qw15n.jpg (堆栈不允许我上传图片)

我有点迷失了......不再知道什么是错的和正确的。在网上尝试了很多解决方案,大部分都了解了,但还是缺少一些东西。

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

最佳答案

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

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

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

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

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

关于java - JAXB - 无法弄清楚如何正确使用 refID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33199267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com