2013-10-28 22 views
1

我有幾個XSD,我通過它們通過XJC創建了Java代碼。我可以通過「直接映射」POJOs XJC提供的許多信息。剩下的大部分可以通過JAXBElements獲取。然而,我不知道如何與他們交談,有幾個因素,即「交易/描述」父母的「成本」因素。JAXB - 無法獲取某些對象信息

<transaction> 
    <aid-type code="1995-09-25"/> 
    <flow-type code="10">ODA</flow-type> 
    <provider-org ref="DE-1">BMZ</provider-org> 
    <value currency="EUR" value-date="1995-09-25">2070227</value> 
    <transaction-type code="D">Disbursement</transaction-type> 
    <description type="1" xml:lang="en"> 
    <costs currency="EUR" type="Personnel costs">1060135</costs> 
    <costs currency="EUR" type="Material costs">665117</costs> 
    <costs currency="EUR" type="Other costs">344975</costs> 
    </description> 
</transaction> 

正如你可以看到Transaction.java包含「描述」元素和地圖對JAXBElement.class。

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
"valueOrDescriptionOrTransactionType" 
}) 
@XmlRootElement(name = "transaction") 
public class Transaction { 

@XmlElementRefs({ 
    @XmlElementRef(name = "transaction-date", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "value", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "disbursement-channel", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "receiver-org", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "transaction-type", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "description", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "tied-status", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "aid-type", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "finance-type", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "provider-org", type = JAXBElement.class, required = false), 
    @XmlElementRef(name = "flow-type", type = JAXBElement.class, required = false) 
}) 
@XmlAnyElement(lax = true) 
protected List<Object> valueOrDescriptionOrTransactionType; 
@XmlAttribute(name = "ref") 
protected String ref; 
@XmlAnyAttribute 
private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

在Transaction.java頂部的模式片段說:

... 
&lt;element name="description" type="{}textType"/> 
... 

那麼的 '描述' 的類型應該是JAXBElement的<TextType>。該TextType.java看起來是這樣的:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "textType", propOrder = {"content"}) 
public class TextType { 

@XmlMixed 
@XmlAnyElement(lax = true) 
protected List<Object> content; 
@XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace") 
protected String lang; 
@XmlAnyAttribute 
private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

我們得到的信息進行交易的我建立一個交易對象並獲取它的內容:

List<Object> listOfTransactionContents = transaction.getValueOrDescriptionOrTransactionType(); 

這給了我,我找了清單JAXBElement對象。

for (Object obj : listOfTransactionContents) 
{ 
    JAXBElement<TextType> jaxbElementTextType = (JAXBElement<TextType>) obj; 
    TextType textType = jaxbElementTextType.getValue(); 
    List<Object> listOftTextTypes = textType.getContent(); 
    .... 

但是,這裏是問題,我不知道如何獲得'成本'元素的內容。上述TextType.java的的getContent()方法,它說:

* Objects of the following type(s) are allowed in the list 
* {@link Element } 
* {@link String } 
* {@link Object } 

的「成本」要素的內容必須存儲在某種名單的,因爲可以有不止一個下「描述」父。

回答

0

我的上帝是什麼旅程......'費用'元素映射到com.sun.org.apache.xerces.internal.dom.ElementNSImpl。我不知道爲什麼...但我現在通過所需的信息。