Moxy xml 与具有单独 xml 路径的虚拟道具绑定
Moxy xmlbinding with virtual props having individual xml-path
是否可以为如下所示的对象创建一个 Moxy xml-bindings oxm 文件。该对象有一个以参数名称为键的 Map,而 Value 将是一个配置值。每个参数都有自己的 xml 路径。问题是我在哪里可以指定虚拟道具的 xml-path?。我可以将密钥用作 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-路径感到困惑。
<?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>
我能够通过以下绑定完成此操作。我选择了 json 绑定,因为这是我实际需要的,但是,它也类似于 XML 绑定。
{
"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()"
}
}
}
}
}
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());
是否可以为如下所示的对象创建一个 Moxy xml-bindings oxm 文件。该对象有一个以参数名称为键的 Map,而 Value 将是一个配置值。每个参数都有自己的 xml 路径。问题是我在哪里可以指定虚拟道具的 xml-path?。我可以将密钥用作 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-路径感到困惑。
<?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>
我能够通过以下绑定完成此操作。我选择了 json 绑定,因为这是我实际需要的,但是,它也类似于 XML 绑定。
{
"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()"
}
}
}
}
}
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());