[jdom-interest] Not sure what I'm doing wrong...
Laurent Bihanic
laurent.bihanic at atosorigin.com
Wed Oct 3 01:15:34 PDT 2001
Hi,
Why is "element.setText( null );" in the else statement of the test on
children? An element can have both text and children.
Maybe you shoudl try someting like:
if ( element != null )
{
element.setText( null );
if ( element.hasChildren() )
{
// Process all child elements recursively
Laurent
Terje Kvambe wrote:
> I had/have the same problem.
> I didn't have any real text in my document, only elements, so I made a
> recursive method that looped through the whole JDOM, cutting out all child
> elements (not text) to a List (using getChildren and detach) and pasting it
> back in using setChildren(List) which replaces all content in the parent
> element. This will remove all text in the element including the extra line
> breaks.
>
> Of course, I tried all the methods in the SAXBuilder and the XMLOutputter
> first (JDOM Beta 7) but I would always have some anoying empty lines left
> (which was a problem because I am sending my final XML to a program that
> doesn't support empty lines in the XML... :)
>
> I'd be happy to see a better solution to this problem...
>
> To clean the whole document, just pass the root element to the following
> method...
>
> Best Regards,
> Terje Kvambe =)
>
> /**
> * Method to remove all text in an element recursively
> *
> *@param element A JDOM Element
> */
> public void removeElementText( Element element )
> {
> if ( element != null )
> {
> if ( element.hasChildren() )
> {
> // Process all child elements recursively
> List childList = element.getChildren();
> Iterator itChildren = childList.iterator();
> while ( itChildren.hasNext() )
> {
> Element currentElement =
> (Element)itChildren.next();
> // Detach the element from the parent (to be
> able to add it later)
> currentElement.detach();
> // Process subelements recursively
> removeElementText( currentElement );
> }
> element.setChildren( childList );
> }
> else
> {
> //Remove all text in this element
> element.setText( null );
> }
> }
> }
>
More information about the jdom-interest
mailing list