JAXB - marshal与java评论 [英] JAXB - marshal with java comments

查看:116
本文介绍了JAXB - marshal与java评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  / * 
*一些注释
* /

//另一个注释

在注释块中以XML格式获取它们:

 <! -  
一些注释
- > ;

<! - 另一个注释 - >

有任何方法可以编辑带有注释的java文件吗?


  1. 解决方案

注释不存储在类的字节代码中,因此您需要在其他位置使其可用。

  • JAXB API不提供映射到内容的方法节点。

  • 话虽这么说,下面是一个方法,可以使用:StAX with JAXB和利用 Marshaller.Listener



    Java模型



      import javax.xml.bind.annotation。*; 

    @XmlRootElement
    @XmlType(propOrder = {name,address})
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Customer {

    private字符串名称;
    private地址地址;

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

    地址地址=新地址(123 A Street);
    客户customer =新客户(Jane,地址);

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

    }

    strong>

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

    格式化输出



    下面是输出的格式:

     <?xml version =1.0?& 
    <! - 之前:forum26802450.Customer@18de9738-->
    <! - 之前:forum26802450.Customer@18de9738-->
    < customer>
    < name> Jane< / name>
    <! - 之前:forum26802450.Address@43e47e37-->
    < address>
    < street> 123 A街< / street>
    <! - 之后:forum26802450.Address@43e47e37-->
    < /地址>
    <! - 之后:forum26802450.Customer@18de9738-->
    < / customer>
    <! - 之后:forum26802450.Customer@18de9738-->


    I am new to JAXB and for the beginning I am marshaling some simple Java files like Hello, world!.

    I want to marshal whole file, even with my comments lines placed like this:

    /*
    * some comment
    */
    
    //another comment
    

    And get them in XML in comment block:

    <!--
    some comment
    -->
    
    <!-- another comment -->
    

    Is there any way to marshal java files with comments?

    解决方案

    There are a few hurdles to overcome with your use case:

    1. The comments aren't stored in the byte code for a class, so you will need to make them available some where else.
    2. The JAXB API doesn't provide a way to map to a content node.

    That being said, below is an approach that may work for you using: StAX with JAXB and leveraging a Marshaller.Listener.

    Java Model

    Customer

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

    Address

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

    Demo Code

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

    Demo

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

    Real Output

    <?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-->
    

    Formatted Output

    Here is what the output looks like formatted:

    <?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-->
    

    这篇关于JAXB - marshal与java评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆