7

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

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

这对 JAXB 可行吗?或者至少有一些特定的 JAXB 实现?

4

1 回答 1

7

您可以利用 anXMLStreamWriter和 anXmlAdapter来执行此操作:

鲍勃适配器

需要注意的事项XmlAdapter

  • 它是有状态的并且引用了XMLStreamWriter. 稍后我们将利用 JAXBXmlAdapterMarshaller.
  • 它从 a 转换List<String>为 a List<String>,我们只是在XmlAdapter这里使用 a 来获取事件。
  • 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 ) 以添加对处理指令的本机支持。这最终将允许您在您的财产上指定以下内容:

@XmlProcessingInstruction(target="array", value="bob")
public List<String> getBob() {
    return bob;
}
于 2011-08-09T18:17:04.200 回答