EclipseLink MOXy:XmlPath注释中的逻辑运算符 [英] EclipseLink MOXy: Logical operators in XmlPath annotation

查看:120
本文介绍了EclipseLink MOXy:XmlPath注释中的逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

逻辑运算符是否适用于EclipseLink MOXy的XmlPath注释?
我试过并且无法使它工作(没有抛出异常,没有任何东西被绑定到元素)。

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>

有没有办法通过修改绑定来实现相同的结果而不使用逻辑或in xml路径?

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

我只能想到一个解决方法,在这个解决方法中,人们会在域模型中使用getter和设置,绑定 / a / b / c / d 到元素并让setter将元素附加到List,而不是在每次调用setElements()时替换列表。不过,我宁愿在绑定文件中处理它。

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.

文档中是否存在指定MOXy中支持哪些XPath部分的地方?

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

推荐答案

以下是如何支持此用例的示例。

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

映射文档(bindings.xml)

您可以使用 xml-elements 映射对于这个用例。在每个嵌套的 xml-element 映射中,您将指定不同的 xml-path

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模型(内容)

下面是我们将用于此示例的Java模型。

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

要将MOXy指定为JAXB提供程序,请包含名为的文件jaxb.properties 在与您的域模型相同的包中使用以下条目(请参阅: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

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.xml)

以下是输入文档示例。

<?xml version="1.0" encoding="UTF-8"?>
<content>
    <a>
        <b/>
        <b/>
    </a>
    <c>
        <d/>
        <d/>
    </c>
</content>

演示

下面是一些可以运行的演示代码,以证明一切正常:

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);
    }

}

输出

由于所有项目属于同一类型,因此它们将根据 xml-path 输出 xml-elements 映射中的第一个 xml-element

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


文档中是否存在指定MOXy中支持哪些
部分XPath的地方?

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

我们将在XPath语句中添加一些验证为映射输入的。您可以使用以下链接跟踪我们的进度:

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

这篇关于EclipseLink MOXy:XmlPath注释中的逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆