开发者

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

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开发者_StackOverflow社区'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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜