如何在JAXB编组期间添加XML处理指令 [英] 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>
这是否适用于JAXB?或者至少使用一些特定的JAXB实现?
Is this possible with JAXB? Or at least with some specific JAXB implementation?
推荐答案
您可以利用 XMLStreamWriter
和 XmlAdapter
执行此操作:
You could leverage an XMLStreamWriter
and an XmlAdapter
to do this:
BobAdapter
关于 XmlAdapter
的注意事项:
- 它是有状态的并且引用了
XMLStreamWriter
。我们稍后将利用JAXB在Marshaller
上设置有状态XmlAdapter
的能力。 - 它从
List< String>
转换为List< String>
,我们只是使用XmlAdapter
这里是为了获得一个活动。 -
marshal
方法是我们在<$ c上调用writeProcessingInstruction
的地方$ c> XMLStreamWriter :
- It's stateful and references an
XMLStreamWriter
. We will later leverage JAXB's ability to set a statefulXmlAdapter
on aMarshaller
. - It converts from a
List<String>
to aList<String>
, we're just using anXmlAdapter
here to get an event. - The
marshal
method is where we callwriteProcessingInstruction
on theXMLStreamWriter
:
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
@XmlJavaTypeAdapter
注释用于链接 XmlAdapter
使用字段/属性:
The @XmlJavaTypeAdapter
annotation is used to link the XmlAdapter
with the field/property:
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;
}
}
演示
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
<?xml version="1.0" encoding="UTF-8"?>
<alice>
<?array bob?>
<bob>edgar</bob>
<bob>david</bob>
</alice>
输出
<?xml version='1.0' encoding='UTF-8'?><alice><?array bob?><bob>edgar</bob><bob>david</bob></alice>
注意
此示例需要使用 EclipseLink JAXB(MOXy)运行,因为JAXB RI将抛出以下异常(我是MOXy领导):
This example needs to be run with EclipseLink JAXB (MOXy) as the JAXB RI will throw the following exception (I'm the MOXy lead):
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
更多信息
- http:// blog .bdoughan.com / 2010/07 / xmladapter-jaxbs-secret-weapon.html
- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
- http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
- http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
更新
我已输入增强请求( https://bugs.eclipse.org/354286)为处理指令添加本机支持。这最终将允许您在您的属性上指定以下内容:
I have entered an enhancement request (https://bugs.eclipse.org/354286) to add native support for processing instructions. This would eventually allow you to specify the following on your property:
@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
return bob;
}
这篇关于如何在JAXB编组期间添加XML处理指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!