With MOXy and XPath, is it possible to unmarshal two lists of attributes?

大城市里の小女人 提交于 2019-12-08 01:38:40

问题


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.

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>

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

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.

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


回答1:


Short Answer

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

Long Answer

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>

but not:

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

Workaround

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

For More Information on @XmlPath

  • 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


来源:https://stackoverflow.com/questions/7101474/with-moxy-and-xpath-is-it-possible-to-unmarshal-two-lists-of-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!