2
votes

We are dealing with a rather complex XML schema (HR-XML) and hoping to use an xpath based mapping approach to unmarshall to our locally defined domain objects. We tried Simple but ran into issues. We've recently tried MOXy with a little better luck but have hit an issue with predicate support. I'm trying to confirm if MOXy supports the predicate that I think I need to use. What I need to do is retrieve the value of one element based on a sibling element's value.

When this is executed I get nulls like it isn't selecting correctly. Has anybody done similar? Maybe there is another issue?

Example XML:

<person>
<communication>
    <address>
        <street>101 First St.</street>
        <city>Whoville</city>
        <state>CA</state>
    </address>
</communication>
<communication>
    <channelcode>email</channelcode>
    <uri>[email protected]</uri>
</communication>
<communication>
    <channelcode>telephone</channelcode>
    <usecode>mobile</usecode>
    <dialnumber>555-555-5555</dialnumber>
</communication>
</person>

Example Obj:

public class Person
{
    private String email;
    private Address homeAddress;
    private String homePhone;
...

Example xml-bindings.xml fragment:

<java-types>
      <java-type name="Person">
        <xml-root-element name="person">
        <java-attributes>
          <xml-element java-attribute="email" xml-path="communication/uri[../channelcode/text()='email']/text()" />
          <xml-element java-attribute="homePhone" xml-path="communication[channelcode/text()='telephone']/dialnumber/text()" />
          <xml-element java-attribute="homeAddress" xml-path="communication/Address" />
        </java-attributes>
     </java-type>
    ...
1

1 Answers

0
votes

Currently EclipseLink JAXB (MOXy) requires that the predicate check the value of an XML attribute. This means if you had the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <communication channelcode="address">
      <address>
         <city>Whoville</city>
         <state>CA</state>
         <street>101 First St.</street>
      </address>
   </communication>
   <communication channelcode="email">
      <uri>[email protected]</uri>
   </communication>
   <communication channelcode="telephone">
      <dialnumber>555-555-5555</dialnumber>
   </communication>
</person>

Then you could map it with the following:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum14368563"
    xml-accessor-type="FIELD">
    <java-types>
        <java-type name="Person">
            <xml-root-element/>
            <xml-type prop-order="homeAddress email homePhone"/>
            <java-attributes>
                <xml-element java-attribute="homeAddress" xml-path="communication[@channelcode='address']/address"/>
                <xml-element java-attribute="email" xml-path="communication[@channelcode='email']/uri/text()"/>
                <xml-element java-attribute="homePhone" name="communication[@channelcode='telephone']/dialnumber/text()"/>
            </java-attributes>
        </java-type>
     </java-types>
</xml-bindings>