[英]JAXB - marshal with java comments
I am new to JAXB and for the beginning I am marshaling some simple Java files like Hello, world!
我是JAXB的新手,一开始我正在编组一些简单的Java文件,比如
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: 并在注释块中以XML格式获取它们:
<!--
some comment
-->
<!-- another comment -->
Is there any way to marshal java files with comments? 有没有办法用注释来编组java文件?
There are a few hurdles to overcome with your use case: 您的用例需要克服几个障碍:
That being said, below is an approach that may work for you using: StAX with JAXB and leveraging a Marshaller.Listener
. 话虽如此,下面是一个可能适用于您的方法:使用JAXB StAX并利用
Marshaller.Listener
。
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;
}
}
MyMarshalListener 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-->
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.