[英]How to add XML processing instructions during JAXB marshal
I would like to add a processing instruction whenever a collection/array property is serialized to get something like 每当集合/数组属性被序列化以获得类似的东西时,我想添加一个处理指令
<alice>
<? array bob ?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
Is this possible with JAXB? JAXB可以实现吗? Or at least with some specific JAXB implementation?
或者至少使用一些特定的JAXB实现?
You could leverage an XMLStreamWriter
and an XmlAdapter
to do this: 您可以利用
XMLStreamWriter
和XmlAdapter
来执行此操作:
BobAdapter BobAdapter
Things to note about the XmlAdapter
: 有关
XmlAdapter
注意XmlAdapter
:
XMLStreamWriter
. XMLStreamWriter
。 We will later leverage JAXB's ability to set a stateful XmlAdapter
on a Marshaller
. Marshaller
上设置有状态XmlAdapter
的能力。 List<String>
to a List<String>
, we're just using an XmlAdapter
here to get an event. List<String>
转换为List<String>
,我们只是在这里使用XmlAdapter
来获取事件。 marshal
method is where we call writeProcessingInstruction
on the XMLStreamWriter
: marshal
方法是我们在XMLStreamWriter
上调用writeProcessingInstruction
的地方: package forum6931520;
import java.util.List;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.stream.XMLStreamWriter;
public class BobAdapter extends XmlAdapter<List<String>, List<String>> {
private boolean first = true;
private XMLStreamWriter xmlStreamWriter;
public BobAdapter() {
}
public BobAdapter(XMLStreamWriter xmlStreamWriter) {
this();
this.xmlStreamWriter = xmlStreamWriter;
}
@Override
public List<String> marshal(List<String> stringList) throws Exception {
if(first) {
xmlStreamWriter.writeProcessingInstruction("array", "bob");
first = false;
}
return stringList;
}
@Override
public List<String> unmarshal(List<String> stringList) throws Exception {
return stringList;
}
}
Alice 爱丽丝
The @XmlJavaTypeAdapter
annotation is used to link the XmlAdapter
with the field/property: @XmlJavaTypeAdapter
注释用于将XmlAdapter
与字段/属性链接:
package forum6931520;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlRootElement
public class Alice {
private List<String> bob;
@XmlJavaTypeAdapter(BobAdapter.class)
public List<String> getBob() {
return bob;
}
public void setBob(List<String> bob) {
this.bob = bob;
}
}
Demo 演示
package forum6931520;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Alice.class);
File xml = new File("src/forum6931520/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Alice alice = (Alice) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(System.out);
marshaller.setAdapter(new BobAdapter(xmlStreamWriter));
marshaller.marshal(alice, xmlStreamWriter);
}
}
input.xml input.xml中
<?xml version="1.0" encoding="UTF-8"?>
<alice>
<?array bob?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
Output 产量
<?xml version='1.0' encoding='UTF-8'?><alice><?array bob?><bob>edgar</bob><bob>david</bob></alice>
Note 注意
This example needs to be run with EclipseLink JAXB (MOXy) as the JAXB RI will throw the following exception (I'm the MOXy lead): 此示例需要使用EclipseLink JAXB(MOXy)运行,因为JAXB RI将引发以下异常(我是MOXy引导):
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
java.util.List is an interface, and JAXB can't handle interfaces.
this problem is related to the following location:
at java.util.List
at public java.util.List forum6931520.Alice.getBob()
at forum6931520.Alice
java.util.List does not have a no-arg default constructor.
this problem is related to the following location:
at java.util.List
at public java.util.List forum6931520.Alice.getBob()
at forum6931520.Alice
For More Information 欲获得更多信息
UPDATE UPDATE
I have entered an enhancement request ( https://bugs.eclipse.org/354286 ) to add native support for processing instructions. 我已输入增强请求( https://bugs.eclipse.org/354286 )以添加对处理指令的本机支持。 This would eventually allow you to specify the following on your property:
这最终将允许您在您的属性上指定以下内容:
@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
return bob;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.