JAXB - 用java评论编组

时间:2014-11-07 13:33:15

标签: java xml jaxb comments

我是JAXB的新手,一开始我正在编组一些简单的Java文件,例如Hello, world!

我想编组整个文件,即使我的评论行放置如下:

/*
* some comment
*/

//another comment

在评论栏中以XML格式获取它们:

<!--
some comment
-->

<!-- another comment -->

有没有办法用注释来编组java文件?

1 个答案:

答案 0 :(得分:15)

使用案例需要克服一些障碍:

  1. 这些评论不会存储在课程的字节代码中,因此您需要在其他地方提供这些评论。
  2. JAXB API没有提供映射到内容节点的方法。
  3. 话虽如此,下面是一种可能适用于您的方法:StAX with JAXB并利用Marshaller.Listener

    Java模型

    <强>客户

    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlType(propOrder={"name", "address"})
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer {
    
        private String name;
        private Address address;
    
        public Customer() {
        }
    
        public Customer(String name, Address address) {
            this.name = name;
            this.address = address;
        }
    
    }
    

    <强>地址

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Address {
    
        String street;
    
        public Address() {
        }
    
        public Address(String street) {
            this.street = street;
        }
    
    }
    

    演示代码

    <强> MyMarshalListener

    import javax.xml.bind.Marshaller;
    import javax.xml.stream.*;
    
    public class MyMarshallerListener extends Marshaller.Listener {
    
        private XMLStreamWriter xsw;
    
        public  MyMarshallerListener(XMLStreamWriter xsw) {
            this.xsw = xsw;
        }
    
        @Override
        public void beforeMarshal(Object source)  {
            try {
                xsw.writeComment("Before:  " + source.toString());
            } catch(XMLStreamException e) {
                // TODO: handle exception
            }
        }
    
        @Override
        public void afterMarshal(Object source) {
            try {
                xsw.writeComment("After:  " + source.toString());
            } catch(XMLStreamException e) {
                // TODO: handle exception
            }
        }
    
    }
    

    <强>演示

    import javax.xml.bind.*;
    import javax.xml.stream.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Customer.class);
    
            Address address = new Address("123 A Street");
            Customer customer = new Customer("Jane", address);
    
            XMLOutputFactory xof = XMLOutputFactory.newFactory();
            XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setListener(new MyMarshallerListener(xsw));
            marshaller.marshal(customer, xsw);
            xsw.close();;
        }
    
    }
    

    实际输出

    <?xml version="1.0" ?><!--Before:  forum26802450.Customer@18de9738--><!--Before:  forum26802450.Customer@18de9738--><customer><name>Jane</name><!--Before:  forum26802450.Address@43e47e37--><address><street>123 A Street</street><!--After:  forum26802450.Address@43e47e37--></address><!--After:  forum26802450.Customer@18de9738--></customer><!--After:  forum26802450.Customer@18de9738-->
    

    格式化输出

    以下是输出的格式:

    <?xml version="1.0" ?>
    <!--Before:  forum26802450.Customer@18de9738-->
    <!--Before:  forum26802450.Customer@18de9738-->
    <customer>
        <name>Jane</name>
        <!--Before:  forum26802450.Address@43e47e37-->
        <address>
            <street>123 A Street</street>
            <!--After:  forum26802450.Address@43e47e37-->
        </address>
        <!--After:  forum26802450.Customer@18de9738-->
     </customer>
     <!--After:  forum26802450.Customer@18de9738-->