[jdom-interest] Not sure what I'm doing wrong...

Terje Kvambe Terje.Kvambe at multimedialab.tv
Tue Oct 2 07:11:24 PDT 2001

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

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

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 =
				// Detach the element from the parent (to be
able to add it later)
				// Process subelements recursively
				removeElementText( currentElement );
			element.setChildren( childList );
			//Remove all text in this element
			element.setText( null );

-----Original Message-----
From: Tim Swantek [mailto:tims at tgf.tc.faa.gov]
Sent: 1. oktober 2001 22:29
To: jdom-interest at jdom.org
Subject: [jdom-interest] Not sure what I'm doing wrong...

    Greetings JDOM,

     I've encountered an interesting delima: not sure how to handle it.
I'm attempting to 1) see if a file named name.xml
exists, if so build a jdom document from the xml located there, 2)
Delete the file,  3) Append additional elements to
the end of the built document, 4) re-write the xml to a file, using the
deleted file name.  The code below will make a
name.xml file for you the first time it is run.  Everything runs
smoothly, when you run the program again, everything
still runs fine, but examining the file shows something is amiss....
take a gander at the code below:

// Test program that duplicates the problem.

import java.lang.*;
import java.io.*;
import java.net.*;

import org.jdom.*;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;

public class TestCase
  public static final void main(String[] args)
     DataOutputStream dos;
     File someNameFile = new File("name.xml");

     Element rootElement = new Element("Names");
     Document nameDocument = new Document(rootElement);

     if(someNameFile.exists())   // If the file exists, gain the root
node from the xml, delete the file afterwards.
         nameDocument = importNamesFromXML(someNameFile.toURL());
         rootElement = nameDocument.getRootElement();
       catch(MalformedURLException mue)
         System.out.println("Malformed URL exception being caught: " +
mue.getMessage());       }

     Element nameElement = addMoreNames();
     rootElement.addContent(nameElement);   // add the new names to the
end of the document.

       dos = new DataOutputStream(new FileOutputStream(someNameFile));
       XMLOutputter xmlOutputter = new XMLOutputter("  ", true);
       xmlOutputter.output(nameDocument, dos);
     catch(FileNotFoundException fnfe)
       System.out.println("File not found exception being caught: " +
fnfe.getMessage());     }
     catch(IOException ioe)
       System.out.println("IOException being caught: " +

   * Add three more elements to an element named: "AddedNames", return
   * "AddedNames" element.
  private static Element addMoreNames()
     Element addedNames = new Element("AddedNames");
     Element firstName = new Element("JoesName");

     Element secondName = new Element("BobsName");

     Element thirdName = new Element("PetesName");

     return addedNames;

   * Returns a JDOM Document built from a url.

  private static Document importNamesFromXML(URL url)
      DOMBuilder nameBuilder = new DOMBuilder(false);
      Document nameDoc = nameBuilder.build(url);
      return nameDoc;
    catch(JDOMException jde)
      System.out.println("A JDOMException is being caught: " +
      return null;

   If you view the output the first time you run the code you'll notice
the name.xml file looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Names>

    If you run the program a second time the name.xml file looks
something like this:

?xml version="1.0" encoding="UTF-8"?>^M
<!DOCTYPE Names>^M







    It looks as if it's tagging the white space after the elements for
removal (or windows car returns), but including it as part of the
element anyway: if you run the program more times you'll notice the
white space growing larger.

    I'm not sure what I'm doing wrong.  (I'm using the Jdom b-7 build).
If it's an error on my part, could you send me an
eMail with a virtual *bonk* on my head so I gain enlightenment?  Thanks
so much for any help you can give.  If I find
out what's wrong I'll be sure to tell you.

To control your jdom-interest membership:

More information about the jdom-interest mailing list