2015-10-18 63 views
0

目標:編組和解組的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(堆棧沒有讓我上傳圖片)

我有點失去了它..不知道什麼是錯的和正確的。嘗試了許多在線解決方案,瞭解其中的大部分,但仍然缺少一些東西。

(這是一所學校的項目和它的尚未重構)

回答

1

它是如何應該可以得到物理治療師(PT)引用到診所對象解編PT列表中有任何對這些診所的對象沒有連接時?診所是根據XML數據構建而成的,期間內沒有PT。

爲使XmlID和XmlIDREF能夠正常工作,即在標註爲XmlIDREF的字段中存儲對象引用,必須存在合適類型的對象,並在同一XML文件內的XmlID字段中具有匹配值。

您必須將XML數據合併到一個文件中。

看到你從診所的PT和PT來的診所,恐怕你會在一個方向上遇到困難。 (我可能是錯的 - 從我嘗試這個以來太長了。)

現在我認爲你可能不想合併XML文件。爲了解決您的困境,我建議您放棄ID和IDREF註釋並「手動」設置鏈接。一次通過PT列表就足夠了,這是一個簡單而強大的解決方案。

+0

謝謝!我們將會處理這些建議。 – JochemQuery