2011-06-17 7 views
1

コメントノードを持つDocument要素があります。私はTransformerを使って文書を整列しようとしましたが、コメントノードは無視されませんでしたが、JAXBを使用してマーシャリングするとき、コメントノードは無視されます。コメントノード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解析 ** * ** *

<?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マーシャリング ** *** *

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

が見えます。また、私はあなたの質問が何であるかを明確にしていません。 JAXB経由でコメントノードにアクセスしますか? –

+0

@Terellドキュメントオブジェクトにコメントノードがありますが、Jaxbを使用してマーシャリングするとコメントノードが見つかりません。 – Jad2006

答えて

3

君は コメントのようなものを維持するためにJAXBにBinder概念を使用することができます:あなたは出力が不足しているよう

+0

これは、オブジェクトの文字列がアンマーシャリングされている場合に機能しますが、私の場合はオブジェクトに@XMLAnyElementがあります。私のオブジェクトクラスpublic class Foo {@XmlAnyElement(lax = true)protected Object any;/** * anyプロパティの値を取得します。 * * @return *可能なオブジェクトは* {@link要素} * {@linkオブジェクト} * */public Object getAny(){任意のものを返します。 } – Jad2006

関連する問題