问题
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?
回答1:
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>
UPDATE
Does there exist a place in the documentation that specifies which parts of XPath are supported in MOXy?
Here are some examples that should help:
- http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
- http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
- http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
We are going to add some validation on the XPath statements that are entered for mappings. You can track our progress on this using the following link:
- http://bugs.eclipse.org/397101
来源:https://stackoverflow.com/questions/17977009/eclipselink-moxy-logical-operators-in-xmlpath-annotation