【问题标题】:How to add XML processing instructions during JAXB marshal如何在 JAXB 编组期间添加 XML 处理指令
【发布时间】:2011-10-19 09:06:00
【问题描述】:

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

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

这可以通过 JAXB 实现吗?或者至少有一些特定的 JAXB 实现?

【问题讨论】:

    标签: java xml jaxb processing-instruction


    【解决方案1】:

    您可以利用XMLStreamWriterXmlAdapter 来执行此操作:

    BobAdapter

    关于XmlAdapter的注意事项:

    • 它是有状态的并引用XMLStreamWriter。稍后我们将利用 JAXB 的能力在 Marshaller 上设置有状态的 XmlAdapter
    • 它从List&lt;String&gt; 转换为List&lt;String&gt;,我们只是在这里使用XmlAdapter 来获取事件。
    • 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;
        }
    
    }
    

    爱丽丝

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

    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 负责人):

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

    【讨论】:

    • 谢谢,我会用 MOXy 试试这个。 @ProcessingInstruction 注释肯定会很好。但是,由于我想为任何列表添加 PI,因此为模型中的所有列表属性添加注释会有些不方便。有没有办法全局注册一个 XmlAdapter?
    • @chris - 您可以使用 @XmlJavaTypeAdapters 在包级别注册 XmlAdapters(请参阅 blog.bdoughan.com/2011/05/…)。但是,由于您将拥有包含不同类型内容的列表,因此这对您不起作用。此外,我猜测 PI 的内容会因每个属性而异,这也意味着每个属性的解决方案会更好。
    • 好的,谢谢。不幸的是, marshal(...) 方法没有关于属性是/元素的任何信息。这意味着我必须为每个列表属性实现一个适配器类。嗯... @ProcessingInstruction 注释会很好。
    • 你如何看待某种 MarshalListener,它可以通过监听器方法添加到 Marshaller 中,通知当前的 marshal 事件?
    • @chris - 我想过,但是MarshalListener 是在域对象级别,你真的需要一个字段/属性级别的事件。
    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2011-11-28
    • 2021-08-09
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多