質問

I have a few XSDs from which I created Java code via XJC. I can get many of the information via the "direct mapping" POJOs XJC provided. Most of the rest can be retrieved via JAXBElements. However there are a few elements from which I just don't know how to speak to them, namely the 'cost' elements under the 'transaction/description' parent.

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

As you can see Transaction.java contains the 'description' element and maps it against 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>();

On top of Transaction.java the schema fragment says:

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

So the type of 'description' should be JAXBElement<TextType>. The TextType.java looks like this:

@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>();

Now to get information out of a transaction I build a transaction object and retrieve it's contents:

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

This gives me a list in which I look for JAXBElement objects.

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

But here is the problem I do not know how to get the contents of the 'cost' elements now. Above the getContent() method of TextType.java it says:

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

The contents of the 'cost' elements must be stored in some kind of list because there can be more than one under a 'description' parent.

役に立ちましたか?

解決 2

My god what a trip...the 'costs' elements are mapped to com.sun.org.apache.xerces.internal.dom.ElementNSImpl. I don't know why...but i get through to the needed information now.

他のヒント

@XmlAnyElement(lax=true) means that if the element encountered corresponds to the root element of a class (mapped with @XmlRootElement or @XmlElementDecl then an instance of that object will be created otherwise the data will be kept as DOM nodes.

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;

For More Information

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top