java - JAXB - 无法获取某些对象信息

标签 java jaxb xjc jaxbelement

我有一些 XSD,我通过 XJC 从这些 XSD 中创建了 Java 代码。我可以通过 XJC 提供的“直接映射”POJO 获取许多信息。其余大部分可以通过 JAXBElements 检索。然而,有一些元素我不知道如何与他们交谈,即“交易/描述”父项下的“成本”元素。

<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 包含“description”元素并将其映射到 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.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();
    ....

但问题是我现在不知道如何获取“cost”元素的内容。在 TextType.java 的 getContent() 方法上方显示:

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

“cost”元素的内容必须存储在某种列表中,因为“description”父级下可能有多个元素。

最佳答案

@XmlAnyElement(lax=true) 表示如果遇到的元素对应于类的根元素(使用 @XmlRootElement@XmlElementDecl 映射,则将创建该对象的实例,否则数据将保留为 DOM 节点。

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

了解更多信息

关于java - JAXB - 无法获取某些对象信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19638177/

相关文章:

java - JAXB Marshal 缺少属性/元素

java - 在STS中使用带有spring boot的jaxb2 maven插件时收到错误

java - 为什么 wsimport 在处理具有 @XmlRootElement 注释的服务器对象时遇到问题?

java - xjc 命令未创建模式中定义的所有类

java - JAXB/xjc 生成的异常类不可抛出

java - 递归 - 如何获得不带小数的整数

java - 使用 Android 传感器测量墙平面和地板平面之间的角度

java - 列出和删除新创建的文件

java - 使用 Ajax 的 HtmlUnit 表单刷新不起作用

java - 使用 Companies House XML 网关搜索公司名称可用性