开发者

Comment Node JaxB Marshalling

I have a Document element with comment node in it. I tried to marshall the document using Transformer, the comment node is not ignored but when marshalling using JAXB, the comment node is ignored.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);


    Document doc = null;

    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = null;
                   String xmlData="<div>SMITH <span id="first"> dsdsds </span><span id="first"><!- it    ends there --></span>adsadsa</div>";
    doc = db.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")));
    DOMSource domSource = new DOMSource(doc.getDocumentElement());
       StringWriter writer = new StringWriter();
       StreamResult result = new StreamResult(writer);
       TransformerFactory tf = TransformerFactory.newInstance();
       Transformer transformer = tf.newTransformer();
       transformer.transform(domSource, result);
       System.out.println("***********DOM PARSING ***************");
       System.out.println(writer.toString());
       System.out.println("***********DOM PARSING ***************");
       Foo foo = new Foo();
       foo.setAny(doc.getDocumentElement());
       System.out.println("***********JAXB MARSHALLING ***************");
       JAXBContext jaxbContext = JAXBContext.newInstance("com.dto");

       Marshaller marshaller = jaxbContext.createMarshaller();
       writer = new StringWriter();
       String itemNamespace="";
        marshaller.marshal(new JAXBElement<Foo>(new QName(
                "", "foo"),
                Foo.class, foo), writer);
       System.out.println(writer.toString());
       System.out.println("***********JAXB MARSHALLING ***************");

Foo object:

public class Foo {

    @XmlAnyElement(lax = true)
    protected Object any;
/**
     * Gets the value of the any property.
     * 
     * @return
     *     possible object is
     *     {@link Element }
     *     {@link Object }
     *     
     */
    public Object getAny() {
        return any;
    }

Input:

   <div>SMITH <a xmlns:locator="http://ntr.lxnx.org" xmlns="http://www.w3.org/1999/xhtml" id="66ad1d3c-b86c-40a5-994f-fd5e39d80ff6" href="javascript:void(0)" class="lx-anchor lx-annotation-anchor" title="annotation starts here"><img src="/Pages/images/IconNotePadSmall.png" alt="annotation starts here" /></a><span xmlns:locator="http://ntr.lxnx.org" xmlns="http://www.w3.org/1999/xhtml" class="highlighted 66ad1d3c-b86c-40a5-994f-fd5e39d80ff6">v. TURNER.I</span><span xmlns:locator="http://ntr.lxnx.org" xmlns="http://www.w3.org/1999/xhtml" id="66ad1d3c-b86c-40a5-994f-fd5e39d80ff6-end" title="annotation ends here" class="lx-hidden"><!--Annotation ends here--></span>n the first volume of the Revised Statutes of New York, pages 445, 446, title 4, will be found the law of the State whose constitutionality was brought into question in this case. The law relates to the marine hospital, then established upon Staten Island, and under the superintendence of a physician and certain commissioners of health.</div> 

Output:

****DOM PARSING ******

<?xml version="1.0" encoding="UTF-8"?><div>SMITH <a xmlns="http://www.w3.org/1999/xhtml" xmlns:locator="http://ntr.lxnx.org" class="lx-anchor lx-annotation-anchor" href="javascript:void(0)" id="66ad1d3c-b86c-40a5-994f-fd5e39d80ff6" title="annotation starts here"><img alt="annotation starts here" src="/Pages/images/IconNotePadSmall.png"/></a><span xmlns="http://www.w3.org/1999/xhtml" xmlns:locator="http://ntr.lxnx.org" class="highlighted 66ad1d3c-b86c-40a5-994f-fd5e39d80ff6">v. TURNER.I</span><span xmlns="http://www.w3.org/1999/xhtml" xmlns:locator="http://ntr.lxnx.org" class="lx-hidden" id="66ad1d3c-b86c-40a5-994f-fd5e39d80ff6-end" title="annotation ends here"><!--Annotation ends here--></span>n the first volume of the Revised Statutes of New York, pages 445, 446, title 4, will be found the law of the State whose constitutionality was brought into question in this case. The law relates to the marine hospital, then established upon Staten Island, and under the superintendence of a physician and certain commissioners of health.</div>

****JAXB MARSHALLING ******

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><foo><div>SMITH <a:a class="lx-anchor lx-annotation-anchor" href="javascript:void(0)" id="66ad1d3c-b86c-40a5-994f-fd5e39d80ff6" title="annotation starts here" xmlns="" xmlns:locator="http://ntr.lxnx.org" xmlns:a="http://www.w3.org/1999/xhtml" xmlns:ns12="http://www.w3.org/1999/xhtml"><ns12:img alt="annotation starts here" src="/Pages/images/IconNotePadSmall.png"/></a:a><span:span class="highlighted 66ad1d3c-b86c-40a5-994f-fd5e39d80ff6" xmlns="" xmlns:locator="http://ntr.lxnx.org" xmlns:ns12="http://www.w3.org/1999/xhtml" xmlns:span="http://www.w3.org/1999/xhtml">v. TURNER.I</span:span><span:span class="lx-hidden" id="66ad1d3c-b86c-40a5-994f-fd5e39d80ff6-end" title="annotation ends here" xmlns="" xmlns:locator="http://ntr.lxnx.org" xmlns:ns12="http://www.w3.org/1999/xhtml" xmlns:span="http://www.w3.org/1999/xhtml"/>n the first volume of th开发者_开发技巧e Revised Statutes of New York, pages 445, 446, title 4, will be found the law of the State whose constitutionality was brought into question in this case. The law relates to the marine hospital, then established upon Staten Island, and under the superintendence of a physician and certain commissioners of health.</div></foo>


You can use the Binder concept in JAXB to preserve things like comments:

  • http://bdoughan.blogspot.com/2010/09/jaxb-xml-infoset-preservation.html
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜