具有虚拟道具的Moxy xmlbinding具有单独的xml路径

时间:2015-06-26 22:40:39

标签: java xml xpath eclipselink moxy

是否可以为对象提供Moxy xml-bindings oxm文件,如下所示。该对象具有一个以参数名称键入的Map,而Value将是一个已配置的值。每个参数都有自己的xml路径。问题是我在哪里可以指定虚拟道具的xml路径?我可以将密钥用作xpath本身。但是,仍然不确定绑定将如何变成......

public class ServiceData{
  String Id;
  String desc;

  @XmlTransient
  private Map<String, Object> parameters= new HashMap<>();

  public Object getCustomProps(String name){
      return parameters.get(name);
  }

  public void putCustomProps(String name, Object value){
    parameters.put(name, value);
  }
}

绑定文件可能如下所示,但是,我仍然不知道如何将xml-path分配给地图中的每个条目。

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="net.blah.blah.domain">
<java-types>
    <java-type name="ServiceData">
        <xml-virtual-access-methods get-method="getCustomProps" set-method="putCustomProps" />
        <xml-root-element name="serviceData"/>
        <java-attributes>
            <xml-element java-attribute="id"
                         type="java.lang.String"
                         xml-path="parameters/group[@name='group1']/parameter[@name='param1']/text()"/>

        </java-attributes>
    </java-type>
</java-types>

1 个答案:

答案 0 :(得分:0)

我能够通过以下绑定完成此操作。我选择了json绑定,因为这是我真正需要的,但它也类似于XML绑定。

&#13;
&#13;
{
  "package-name": "net.company.demo",
  "xml-schema": {
    "element-form-default": "QUALIFIED",
    "namespace": "http://www.example.com/customer"
  },
  "java-types": {
    "java-type": {
      "name": "Pojo",
      "xml-virtual-access-methods": {
        "get-method": "get",
        "set-method": "set"
      },
      "xml-root-element": {
        "name": "rootElement"
      },
      "java-attributes": {
        "xml-element": {
          "java-attribute": "message",
          "type": "java.lang.String",
          "xml-path": "/params/parameter[@name='message']/text()"
        },
        "xml-element": {
          "java-attribute": "shouldValidate",
          "type": "java.lang.String",
          "xml-path": "/params/parameter[@name='validate']/text()"
        },
        "xml-element": {
          "java-attribute": "buffer",
          "type": "java.lang.String",
          "xml-path": "/params/parameter[@name='buffer']/text()"
        }
      }
    }
  }
}
&#13;
&#13;
&#13;

Pojo是

public class Pojo{

private String id;

private Map<String, Object> data = new HashMap<String, Object>();

public Object get(String name){
    return data.get(name);
}

public void set(String name, Object value){
    data.put(name, value);
}

}

应用程序代码类似于

        final Map<String, Object> jaxbContextProps = new HashMap<String, Object>();
    jaxbContextProps.put(JAXBContextProperties.OXM_METADATA_SOURCE, "binding.json");
    jaxbContextProps.put(JAXBContextProperties.MEDIA_TYPE, "application/xml");

    final Map<String, Object> marshallerProps = new HashMap<String, Object>();
    marshallerProps.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setLazyInit(true);
    marshaller.setClassesToBeBound(Pojo.class);
    marshaller.setJaxbContextProperties(jaxbContextProps);
    marshaller.setMarshallerProperties(marshallerProps);

    Pojo pojo = new Pojo();
    pojo.set("message", "This is some message");
    pojo.set("shouldValidate", "Yes");
    pojo.set("buffer", "1000");

    StringWriter writer = new StringWriter();
    StreamResult streamResult = new StreamResult(writer);
    marshaller.marshal(pojo,  streamResult);
    //System.out.println(writer.toString());