gpt4 book ai didi

java - 如何在 JAXB marshal 期间添加 XML 处理指令

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:15:02 24 4
gpt4 key购买 nike

每当序列化集合/数组属性以获得类似的东西时,我想添加一条处理指令

<alice>
<? array bob ?>
<bob>edgar</bob>
<bob>david</bob>
</alice>

JAXB 可以做到这一点吗?或者至少使用一些特定的 JAXB 实现?

最佳答案

您可以利用 XMLStreamWriter和一个 XmlAdapter这样做:

BobAdapter

关于 XmlAdapter 的注意事项:

  • 它是有状态的并且引用了一个 XMLStreamWriter .稍后我们将利用 JAXB 的能力来设置有状态的 XmlAdapter。在 Marshaller 上.
  • 它从 List<String> 转换而来到 List<String> , 我们只是使用 XmlAdapter在这里得到一个事件。
  • marshal方法是我们调用 writeProcessingInstruction 的地方在 XMLStreamWriter 上:

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;
}

}

爱丽丝

@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;
}

}

演示

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);
}

}

输入.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 的负责人):

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

了解更多信息


更新

我已输入增强请求 ( https://bugs.eclipse.org/354286 ) 以添加对处理指令的 native 支持。这最终将允许您在您的属性(property)上指定以下内容:

@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
return bob;
}

关于java - 如何在 JAXB marshal 期间添加 XML 处理指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6931520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com