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? Or at least with some specific JAXB implementation?
JAXB能做到这一点吗?或者至少使用某些特定的JAXB实现?
1 个解决方案
#1
6
You could leverage an XMLStreamWriter
and an XmlAdapter
to do this:
您可以利用XMLStreamWriter和XmlAdapter来实现这一点:
BobAdapter
BobAdapter
Things to note about the XmlAdapter
:
关于XmlAdapter需要注意的事项:
- It's stateful and references an
XMLStreamWriter
. We will later leverage JAXB's ability to set a statefulXmlAdapter
on aMarshaller
. - 它是有状态的,并引用XMLStreamWriter。稍后,我们将利用JAXB在编组器上设置有状态的XmlAdapter的能力。
- It converts from a
List<String>
to aList<String>
, we're just using anXmlAdapter
here to get an event. -
它从列表
转换为列表 ,我们只是在这里使用XmlAdapter来获取事件。 - The
marshal
method is where we callwriteProcessingInstruction
on theXMLStreamWriter
: - marshal方法在XMLStreamWriter上调用writeprocessinginstructions:
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
的更多信息
- http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
- 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/2011/05/specifying-eclipselink-moxy-as-your.html
UPDATE
更新
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:
我输入了一个增强请求(https://bugs.eclipse.org/354286)来为处理指令添加本地支持。这将最终允许您在您的财产上指定以下内容:
@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
return bob;
}
#1
6
You could leverage an XMLStreamWriter
and an XmlAdapter
to do this:
您可以利用XMLStreamWriter和XmlAdapter来实现这一点:
BobAdapter
BobAdapter
Things to note about the XmlAdapter
:
关于XmlAdapter需要注意的事项:
- It's stateful and references an
XMLStreamWriter
. We will later leverage JAXB's ability to set a statefulXmlAdapter
on aMarshaller
. - 它是有状态的,并引用XMLStreamWriter。稍后,我们将利用JAXB在编组器上设置有状态的XmlAdapter的能力。
- It converts from a
List<String>
to aList<String>
, we're just using anXmlAdapter
here to get an event. -
它从列表
转换为列表 ,我们只是在这里使用XmlAdapter来获取事件。 - The
marshal
method is where we callwriteProcessingInstruction
on theXMLStreamWriter
: - marshal方法在XMLStreamWriter上调用writeprocessinginstructions:
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
的更多信息
- http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
- 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/2011/05/specifying-eclipselink-moxy-as-your.html
UPDATE
更新
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:
我输入了一个增强请求(https://bugs.eclipse.org/354286)来为处理指令添加本地支持。这将最终允许您在您的财产上指定以下内容:
@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
return bob;
}