2

Using EclipseLink MOXy JAXB implementation, I'm trying to use @XmlPath annotation to get element values based on the value of an attribute of the element. I can't seem to get it to work. Is this supported?

XML Excerpt:
<Item>
  ...
  <ItemRefFields>
    <ItemRefField id="1">12345</ItemRefField>
    <ItemRefField id="2">blah</ItemRefField>
  </ItemRefFields>
</Item>

POJO Excerpt:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Item")
public class Item
{
  ...
  @XmlPath("ItemRefFields/ItemRefField[@id='1']/text()")
  private String ItemRef1 = null;
  @XmlPath("ItemRefFields/ItemRefField[@id='2']/text()")
  private String ItemRef2 = null;
  ...
}

What happens now is that both values are assigned to ItemRef2 in succession such that "blah" ends up being the final value, but ItemRef1 never gets a value assigned. I believe this is because the attribute value part of the XPath expression ([@id='x']) is being ignored. So both XPath expressions are treated as the same and it appears that this is causing the expression to get mapped first to ItemRef1, then to ItemRef2, with ItemRef2 overwriting ItemRef1 mapping, so ItemRef2 wins.

I'm hoping this is being caused by a syntactical issue on my part. Any advice would be appreciated.

Thanks, Kevin

  • Hi, Thanks for this question. actually, I am also trying to do something similar but for some reason, it's not working as expected. I tried the things mentioned in this answer as well. I tried this approach and the example from the authors blog but still the `JAXB Marshalling` does not work as expected with `@XmlPath`. Seems like the XML produced with and without `@XmlPath` are the same. Can you please once look into this example and provide your answer: https://stackoverflow.com/questions/67500565/xmlpath-has-no-impact-during-the-jaxb-marshalling – BATMAN_2008 May 12 '21 at 10:27

1 Answers1

1

I lead EclipseLink JAXB (MOXy), and this feature is part of the upcoming EclipseLink 2.3 release. You can try it today by downloading one of the EclipseLink 2.3.0 nightly downloads (starting March 22) from:

The mapping will be just as you described in your question:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Item")
public class Item
{
  ...
  @XmlPath("ItemRefFields/ItemRefField[@id='1']/text()")
  private String ItemRef1 = null;
  @XmlPath("ItemRefFields/ItemRefField[@id='2']/text()")
  private String ItemRef2 = null;
  ...
}

For more information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • @Kevin James - This feature is now available in the EclipseLink 2.3.0 stream. I have updated my answer with the details. – bdoughan Mar 22 '11 at 13:21
  • Thanks a ton, Blaise, exactly what I needed...and well timed :) Any estimate when 2.3.0 will be ready for primetime? – Kevin James Mar 25 '11 at 17:45
  • EclipseLink 2.3.0 will align with the Eclipse Indigo release this summer: http://www.eclipse.org/projects/timeline/index.php?projectid=rt.eclipselink – bdoughan Mar 25 '11 at 18:00
  • Dear @BlaiseDoughan could you please help me with this http://stackoverflow.com/questions/27765087/xmlpath-break-the-unmarchaling-of-xmlelement, it looks like a bug but maybe a miss config – Nassim MOUALEK Jan 04 '15 at 13:57