Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » EclipseLink MOXy: Logical operators in XmlPath annotation
EclipseLink MOXy: Logical operators in XmlPath annotation [message #1076643] Wed, 31 July 2013 17:28 Go to next message
Tim Braun is currently offline Tim BraunFriend
Messages: 8
Registered: July 2013
Junior Member
Do logical operators work in XmlPath annotations of EclipseLink MOXy? I tried and could not make it work (no Exception is thrown and nothing is bound to "elements").

For example, I would like to have in a bindings file something like this:
    <java-type name="Content">
        <java-attributes>
           <xml-element java-attribute="elements" xml-path="/a/b/ | /c/d" 
            type="ElementType" container-type="java.util.List" />
        </java-attributes>
    </java-type>

Is there a way to achieve the same result from a modification of the bindings without using the logical or in the xml-path?

I can only think of a workaround where one would use getters and settings in the domain model, bind both
/a/b
and
/c/d
to elements and have the setters append elements to the List rather then replacing the list upon each call to setElements(). I'd rather handle it in the bindings file, though.

Does there exist a place in the documentation that specifies which parts of XPath are supported in MOXy?

[Updated on: Wed, 31 July 2013 17:40]

Report message to a moderator

Re: EclipseLink MOXy: Logical operators in XmlPath annotation [message #1076675 is a reply to message #1076643] Wed, 31 July 2013 18:28 Go to previous message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

Here is an example of how you could support this use case.

Mapping Document (bindings.xml)

You could use the xml-elements mapping for this use case. On each of the nested xml-element mappings you would specify a different xml-path.

<?xml version="1.0"?>
<xml-bindings 
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum17977009">
    <java-types>
        <java-type name="Content">
            <xml-root-element/>
             <java-attributes>
                <xml-elements java-attribute="elements">
                    <xml-element xml-path="a/b"/>
                    <xml-element xml-path="c/d"/>
                </xml-elements>
             </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>


Java Model (Content)

Below is the Java model we will use for this example.
package forum17977009;

import java.util.List;

public class Content {

    private List<ElementType> elements;

    public List<ElementType> getElements() {
        return elements;
    }

    public void setElements(List<ElementType> elements) {
        this.elements = elements;
    }


}

jaxb.properties

To specify MOXy as your JAXB provider you include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Input (input.xml)

Below is a sample input document.
<?xml version="1.0" encoding="UTF-8"?>
<content>
    <a>
        <b/>
        <b/>
    </a>
    <c>
        <d/>
        <d/>
    </c>
</content>

Demo

Below is some demo code you can run to prove that everything works:
package forum17977009;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum17977009/bindings.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Content.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum17977009/input.xml");
        Content content = (Content) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(content, System.out);
    }

}

Output

Since all of the items are of the same type, they will output based on the xml-path of the first xml-element in the xml-elements mapping:
<?xml version="1.0" encoding="UTF-8"?>
<content>
   <a>
      <b/>
      <b/>
      <b/>
      <b/>
   </a>
</content>
Previous Topic:EclipseLink MOXy exception due to package structure
Next Topic:MOXy: Override rules of binding files
Goto Forum:
  


Current Time: Fri Dec 19 11:56:18 GMT 2014

Powered by FUDForum. Page generated in 0.03609 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software