逻辑运算符在 EclipseLink MOXy 的 XmlPath 注释中工作吗? 我尝试过但无法使其工作(没有抛出异常,并且没有任何内容绑定(bind)到“元素”)。
例如,我想在绑定(bind)文件中包含如下内容:
<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>
有没有一种方法可以通过修改绑定(bind)来实现相同的结果,而无需在 xml 路径中使用逻辑或?
我只能想到一种解决方法,即在域模型中使用 getter 和设置,将 /a/b
和 /c/d
绑定(bind)到元素,然后让 setter 将元素附加到列表中,而不是在每次调用 setElements() 时替换列表。不过,我宁愿在绑定(bind)文件中处理它。
文档中是否有一个地方指定 MOXy 支持 XPath 的哪些部分?
最佳答案
以下是如何支持此用例的示例。
映射文档 (bindings.xml)
对于此用例,您可以使用 xml-elements
映射。在每个嵌套的 xml-element
映射上,您可以指定不同的 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 模型(内容)
下面是我们将用于此示例的 Java 模型。
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
要将 MOXy 指定为您的 JAXB 提供程序,您可以在与您的域模型相同的包中包含一个名为 jaxb.properties
的文件,其中包含以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
输入(input.xml)
下面是一个示例输入文档。
<?xml version="1.0" encoding="UTF-8"?>
<content>
<a>
<b/>
<b/>
</a>
<c>
<d/>
<d/>
</c>
</content>
演示
下面是一些演示代码,您可以运行来证明一切正常:
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);
}
}
输出
由于所有项目都属于同一类型,因此它们将根据 xml- 中第一个
映射:xml-element
的 xml-path
进行输出元素
<?xml version="1.0" encoding="UTF-8"?>
<content>
<a>
<b/>
<b/>
<b/>
<b/>
</a>
</content>
更新
Does there exist a place in the documentation that specifies which parts of XPath are supported in MOXy?
以下是一些应该有所帮助的示例:
- 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
我们将对为映射输入的 XPath 语句添加一些验证。您可以使用以下链接跟踪我们在这方面的进展:
关于xpath - EclipseLink MOXy : Logical operators in XmlPath annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17977009/