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

Tim Swantek tims at tgf.tc.faa.gov
Mon Oct 1 13:29:22 PDT 2001


    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.
     {
       try
       {
         nameDocument = importNamesFromXML(someNameFile.toURL());
         rootElement = nameDocument.getRootElement();
         someNameFile.delete();
       }
       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.

     try
     {
       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: " +
ioe.getMessage());
     }
  }


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

     Element secondName = new Element("BobsName");
     secondName.addContent("Bob");
     addedNames.addContent(secondName);

     Element thirdName = new Element("PetesName");
     thirdName.addContent("Pete");
     addedNames.addContent(thirdName);

     return addedNames;
  }


  /**
   * Returns a JDOM Document built from a url.
   */

  private static Document importNamesFromXML(URL url)
  {
    try
    {
      DOMBuilder nameBuilder = new DOMBuilder(false);
      Document nameDoc = nameBuilder.build(url);
      return nameDoc;
    }
    catch(JDOMException jde)
    {
      System.out.println("A JDOMException is being caught: " +
jde.getMessage());
      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>
<Names>
  <AddedNames>
    <JoesName>Joe</JoesName>
    <BobsName>Bob</BobsName>
    <PetesName>Pete</PetesName>
  </AddedNames>
</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
<Names>^M

  <AddedNames>^M

    <JoesName>Joe</JoesName>^M

    <BobsName>Bob</BobsName>^M

    <PetesName>Pete</PetesName>^M

  ^M
  </AddedNames>^M

<AddedNames>^M
    <JoesName>Joe</JoesName>^M
    <BobsName>Bob</BobsName>^M
    <PetesName>Pete</PetesName>^M
  </AddedNames>^M
</Names>^M
^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.




More information about the jdom-interest mailing list