[jdom-interest] Getting comments in a Document

David Patterson dpatterson at i-a-i.com
Thu May 11 05:55:29 PDT 2006


Jason,
Thanks for the help. I changed my code to getDescendants() and it worked
correctly. I can now see the comments.

But, the next problem surfaces. I find an Element node, create a new
Document with that node as the root, and transform it into an HTML stream.
The transformed HTML stream does not have comments in it. There is also no
indication from the feedback generated during the transformation that
comments are being seen.  Here is my code:

    Iterator commentIterator;
    Filter comments = new ContentFilter( ContentFilter.COMMENT );
    File inXslt = new File( xsltfn );
    File inDir = new File( sourceDir );
    File in = new File( inDir, fname );
    Document doc = JdomAssistant.docMaker( in );
    for ( commentIterator = doc.getDescendants( comments);
         commentIterator.hasNext();)
    {
        Comment comment = (Comment) commentIterator.next();
        System.out.println( "comment: " + comment.getText() );
    }
    Element root = doc.getRootElement();
    Element prelreqs = (Element) XPath.selectSingleNode( root,
           "/dmodule/content/proced/prelreqs");
    Document prelDoc = new Document( (Element) prelreqs.clone() );
    XSLTransformer tran = new XSLTransformer( inXslt );
    Document newPrelDoc = tran.transform( prelDoc );
    System.out.println( "After transform: " );
    for ( commentIterator = newPrelDoc.getDescendants( comments);
          commentIterator.hasNext();)
    {
        Comment comment = (Comment) commentIterator.next();
        System.out.println( "comment: " + comment.getText() );
    }

I see the comments in the first for loop, and none in the second. Does the
default transformer pass comments through?

Dave Patterson 

-----Original Message-----
From: Jason Hunter [mailto:jhunter at xquery.com] 
Sent: Wednesday, May 10, 2006 6:08 PM
To: David Patterson
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] Getting comments in a Document

getContent() only works one level deep.  getDescendants() might work 
better for you (it returns a lazy Iterator), or an XPath like //comment().

-jh-




More information about the jdom-interest mailing list