目標:編組和解組的clinic.xml正確JAXB - 無法弄清楚如何使用REFID正確
問題:讀出的物理治療師的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>
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;
}
}
終於unma編號部分:
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();
}
這兩個unmarshallers都盡其所能。我從解組2的物理治療師的一個很好的列表,但我沒有關於從診所解組的物理治療師得到任何東西:
http://imgur.com/Mpcgm8t(堆棧沒有讓我上傳圖片)
我有點失去了它..不知道什麼是錯的和正確的。嘗試了許多在線解決方案,瞭解其中的大部分,但仍然缺少一些東西。
(這是一所學校的項目和它的尚未重構)
謝謝!我們將會處理這些建議。 – JochemQuery