评论节点JaxB编组

时间:2011-06-17 18:45:57

标签: java jaxb xml-parsing

我有一个带有注释节点的Document元素。我尝试使用Transformer对文档进行编组,但注释节点不会被忽略,但在使用JAXB进行编组时,注释节点将被忽略。

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对象:

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

输入:

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

输出:

* *** 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 ** < EM> * ** *

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

1 个答案:

答案 0 :(得分:3)

您可以在JAXB中使用Binder概念来保留评论等内容: