[jdom-interest] RE: Whitespace

Jeremy Whitlock jwhitlock at starprecision.com
Mon Jan 19 15:44:17 PST 2004


JDOM,
	I've found the problem and it happens to be with JDOM itself.
If you read in an XML document into JDOM and then output it to a new
file without modifying the structure of the XML Document while in JDOM,
it adds extra characters to the file.  Here's an example:

Here is my document as is on file:

<?xml version="1.0" encoding="UTF-8"?>
<!--DBPirate Configuration File-->
<dbpirate>
   <connections>
      <connection> 
         <name>Oracle</name>
         <user>cbuser</user>
         <password>cbpass</password>
         <hostname>localhost</hostname>
         <port>1521</port>
         <timestamp>345678</timestamp>
         <class>org.mysql.jdbc.Driver</class>
         <url>jdbc:mysql at localhost:3306:cookbook:cbuser:cbpass</url>
         <sid>cookbook</sid>  
      </connection>
      <connection> 
         <name>MySQL</name>
         <user>scott</user>
         <password>tiger</password>
         <hostname>localhost</hostname>
         <port>1521</port>
         <timestamp>12345888</timestamp>
         <class>oracle.jdbc.OracleDriver</class>
         <url>jdbc:oracle:thin at localhost:1521:orcl:scott:tiger</url>
         <sid>orcl</sid>  
      </connection>
   </connections>
</dbpirate>

And here it is after reading it into JDOM and then directly writing it
back out to another file without modifying it:

<?xml version="1.0" encoding="UTF-8"?>
<!--DBPirate Configuration File-->
<dbpirate>
  
   
  <connections>
    
      
    <connection>
       
         
      <name>Oracle</name>
      
         
      <user>cbuser</user>
      
         
      <password>cbpass</password>
      
         
      <hostname>localhost</hostname>
      
         
      <port>1521</port>
      
         
      <timestamp>345678</timestamp>
      
         
      <class>org.mysql.jdbc.Driver</class>
      
         
      <url>jdbc:mysql at localhost:3306:cookbook:cbuser:cbpass</url>
      
         
      <sid>cookbook</sid>
        
      
    </connection>
    
      
    <connection>
       
         
      <name>MySQL</name>
      
         
      <user>scott</user>
      
         
      <password>tiger</password>
      
         
      <hostname>localhost</hostname>
      
         
      <port>1521</port>
      
         
      <timestamp>12345888</timestamp>
      
         
      <class>oracle.jdbc.OracleDriver</class>
      
         
      <url>jdbc:oracle:thin at localhost:1521:orcl:scott:tiger</url>
      
         
      <sid>orcl</sid>
        
      
    </connection>
    
   
  </connections>
  

</dbpirate>


You can see my dilemma.  It messes everything up when I try to delete a
portion of the document and then recreate it as needed and then output
it to a file.  Anyone got any idea why this happens and how to fix it?
Thanks, Jeremy

-----Original Message-----
From: Jeremy Whitlock [mailto:jwhitlock at starprecision.com] 
Sent: Monday, January 19, 2004 3:44 PM
To: JDOM (jdom-interest at jdom.org)
Subject: Whitespace

JDOM,
	I’m trying to clear all nodes of a particular type so here is my
code:

Try
{
	SAXBuilder builder = new SAXBuilder();
	InputStream ins =
ClassLoader.getSystemResourceAsStream("testlogins.conf"); // was
dbpirate.conf
	Document doc = builder.build (ins);
	Element rootElement = doc.getRootElement();
	Element connElement = rootElement.getChild("connections");
	List children = connElement.getChildren("connection");
	Iterator iterator = children.iterator();
			
	rootElement.getChildren("connections").clear();
	rootElement.addContent(new Comment("Testing"));
	rootElement.addContent(new Element("connections"));
	try
	{
		XMLOutputter outputter = new XMLOutputter("  ", true);

	
		outputter.output(doc, System.out);
	}
	catch (java.io.IOException e)
	{
		e.printStackTrace();
	}
}

and I get output like this:

<?xml version="1.0" encoding="UTF-8"?>
<!--DBPirate Configuration File-->
<dbpirate>
  
   

  <!--Testing-->
  <connections />
</dbpirate>

You’ll notice about 3 lines of white space before the comment in the
<dbpirate> element.  Is there any way to get rid of this or a better way
to get rid of nodes so that I can keep the white space out?  Thanks,

Jeremy Whitlock --- MCP/MCSA
IT Manager for Star Precision, Inc.
Phone:  (970) 535-4795
Metro:  (303) 926-0559
Fax:  (970) 535-0780
Metro Fax:  (303) 926-8557
http://www.starprecision.com 





More information about the jdom-interest mailing list