xml - JAXB - marshal with java comments -
xml - JAXB - marshal with java comments -
i new jaxb , origin marshaling simple java files hello, world!
.
i want marshal whole file, comments lines placed this:
/* * comment */ //another comment
and them in xml in comment block:
<!-- comment --> <!-- comment -->
is there way marshal java files comments?
there few hurdles overcome utilize case:
the comments aren't stored in byte code class, need create them available else. the jaxb api doesn't provide way map content node.that beingness said, below approach may work using: stax jaxb , leveraging marshaller.listener
.
customer
import javax.xml.bind.annotation.*; @xmlrootelement @xmltype(proporder={"name", "address"}) @xmlaccessortype(xmlaccesstype.field) public class client { 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) { seek { xsw.writecomment("before: " + source.tostring()); } catch(xmlstreamexception e) { // todo: handle exception } } @override public void aftermarshal(object source) { seek { 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 street"); client 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 street</street><!--after: forum26802450.address@43e47e37--></address><!--after: forum26802450.customer@18de9738--></customer><!--after: forum26802450.customer@18de9738-->
formatted output
here output looks formatted:
<?xml version="1.0" ?> <!--before: forum26802450.customer@18de9738--> <!--before: forum26802450.customer@18de9738--> <customer> <name>jane</name> <!--before: forum26802450.address@43e47e37--> <address> <street>123 street</street> <!--after: forum26802450.address@43e47e37--> </address> <!--after: forum26802450.customer@18de9738--> </customer> <!--after: forum26802450.customer@18de9738-->
java xml jaxb comments
Comments
Post a Comment