使用MOXy和XPath,是否可以解组两个属性列表? [英] With MOXy and XPath, is it possible to unmarshal two lists of attributes?

查看:131
本文介绍了使用MOXy和XPath,是否可以解组两个属性列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这与我提出的另一个问题没有重复,使用MOXy和XPath,是否可以解组属性列表?它类似,但不一样。

Note, this is not a duplicate of another question I asked, "With MOXy and XPath, is it possible to unmarshal a list of attributes?" It's similar, but not the same.

我的XML看起来像这样:

I've got XML that looks like this:

<test>
  <items>
    <item type="cookie" brand="oreo">cookie</item>
    <item type="crackers" brand="ritz">crackers</item>
  </items>
</test>

这与我之前问题中的xml类似,不同的是现在每个项目有两个属性而不是一个。

This is similar to the xml in my earlier question except now there are two attributes per item instead of one.

在我班上:

In my class:

@XmlPath("items/item/@type")
@XmlAttribute
private ArrayList<String> itemList = new ArrayList<String>();
@XmlPath("items/item/@brand")
@XmlAttribute
private ArrayList<String> brandList = new ArrayList<String>();

感谢上一个问题的回答,我可以解组类型属性进入列表。但是, brandList 是空的。如果我注释掉 itemList 的注释(因此它没有被JAXB / MOXy填充),那么 brandList 包含正确的值。

Thanks to the answer to my previous question I am able to unmarshal the type attribute into the list. brandList, however, is empty. If I comment out annotations for itemList (so it is not populated by JAXB/MOXy) then brandList contains the correct values.

看来我只能使用XPath将单个属性解组到列表中。这是设计还是我配置错了?

It appears that I can only unmarshal a single attribute into a list using XPath. Is this by design or have I configured something wrong?

更新:我似乎无法解析文本和元素中的属性无论是。如果我的类映射如下:

Update: It seems I can't unmarshal the text and an attribute from an element either. If my class is mapped like this:

@XmlPath("items/item/text()")
@XmlElement
private ArrayList<String> itemList = new ArrayList<String>();
@XmlPath("items/item/@brand")
@XmlAttribute
private ArrayList<String> brandList = new ArrayList<String>();

brandList 在这种情况下也是空的。如果我切换订单并先映射 brandList ,那么 itemList 为空。就好像第一个映射使用元素一样,因此无法读取基于该元素或其属性的更多值。

brandList is also empty in this case. If I switch the order and map brandList first then itemList is empty. It's as if the first mapping consumes the element so further values based on that element or its attributes cannot be read.

推荐答案

简答

这不是 EclipseLink MOXy 。我已为此输入以下增强请求,请随时添加其他信息以对此错误进行投票:

This isn't a use case that is currently supported with @XmlPath in EclipseLink MOXy. I have entered the following enhancement request for this, feel free to add additional information to to vote for this bug:

  • https://bugs.eclipse.org/355225

长答案

MOXy将支持映射:

MOXy will support mapping:

@XmlPath("items/item/@type")
private ArrayList<String> itemList = new ArrayList<String>();

to:

<test>
  <items>
    <item type="cookie"/>
    <item type="crackers"/>
  </items>
</test>

但不是:

@XmlPath("items/item/@type")
private ArrayList<String> itemList = new ArrayList<String>();

@XmlPath("items/item/@brand")
private ArrayList<String> brandList = new ArrayList<String>();

to:

<test>
  <items>
    <item type="cookie" brand="oreo"/>
    <item type="crackers" brand="ritz"/>
  </items>
</test>

解决方法

你可以引入一个中间对象( Item )来映射这个用例:

You could introduce an intermediate object (Item) to map this use case:

@XmlElementWrapper(name="items")
@XmlElement(name="item")
private ArrayList<Item> itemList = new ArrayList<Item>();

 

public class Item {

    @XmlAttribute
    private String type;

    @XmlAttribute
    private String brand;
}

有关 @XmlPath <的更多信息/ code>

  • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
  • http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
  • http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
  • http://blog.bdoughan.com/2011/08/binding-to-json-xml-geocode-example.html

这篇关于使用MOXy和XPath,是否可以解组两个属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆