[jdom-interest] Bug in CDATA reading/writing?

Mark Roder roder at is.com
Wed Mar 14 13:00:51 PST 2001


I am using JDOM-B6.

I am noticing if I read a file, write a file and then read it back in again,
I get different data the second time I read it when the file contains CDATA.

This seems weird to me.  Is this a bug in the code or something that should
be documented some more?  It was surprising to me.



The XML file:
<?xml version="1.0" encoding="iso-8859-1"?>
<RootElement>
   <ValueOne>This is ValueOne</ValueOne>
   <ValueTwo><![CDATA[This is ValueTwo]]></ValueTwo>
</RootElement>

The Code:  //Change the XMLOutputter used to show the issue.


import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;

public class DataReader
{

    public static void main(String args[])
    {
        try
        {
            //Read one time
        SAXBuilder aBuilderR1 = new SAXBuilder();
        Document aDocR1 = aBuilderR1.build(new File("Sample.xml"));
        Element rootElementR1 = aDocR1.getRootElement();
        Element valueOneR1 = rootElementR1.getChild("ValueOne");
        Element valueTwoR1 = rootElementR1.getChild("ValueTwo");
        System.out.println("Start");        
        System.out.println("R1V1 - getText: " + valueOneR1.getText());
        System.out.println("R1V1 - getTextTrim: " +
valueOneR1.getTextTrim());
        System.out.println("R1V2 - getText: " + valueTwoR1.getText());
        System.out.println("R1V2 - getTextTrim: " +
valueTwoR1.getTextTrim());
        System.out.println("Done");

        //Breaks  things on next line
        //XMLOutputter xmlOut = new XMLOutputter("",true);        
        //XMLOutputter xmlOut = new XMLOutputter("  ",true);

        
        //Breaks  adds the spaces to the front
        //XMLOutputter xmlOut = new XMLOutputter("  ");
        //XMLOutputter xmlOut = new XMLOutputter("  ",false);
        
        //Works
        //XMLOutputter xmlOut = new XMLOutputter("");        
        //XMLOutputter xmlOut = new XMLOutputter("",false);
        XMLOutputter xmlOut = new XMLOutputter();

        
        FileWriter aWriter = new FileWriter("Sample2.xml");
        xmlOut.output(aDocR1,aWriter);
        aWriter.close();


        SAXBuilder aBuilderR2 = new SAXBuilder();
        Document aDocR2 = aBuilderR2.build(new File("Sample2.xml"));
        Element rootElementR2 = aDocR2.getRootElement();
        Element valueOneR2 = rootElementR2.getChild("ValueOne");
        Element valueTwoR2 = rootElementR2.getChild("ValueTwo");
        System.out.println("Start");        
        System.out.println("R2V1 - getText: " + valueOneR2.getText());
        System.out.println("R2V1 - getTextTrim: " +
valueOneR2.getTextTrim());
        System.out.println("R2V2 - getText: " + valueTwoR2.getText());
        System.out.println("R2V2 - getTextTrim: " +
valueTwoR2.getTextTrim());
        System.out.println("Done");

        
        }
        catch(Exception e)
        {
            System.out.println("Exception is " + e);
        }
        
    }
}

Example Error Output:
Start
R1V1 - getText: This is ValueOne
R1V1 - getTextTrim: This is ValueOne
R1V2 - getText: This is ValueTwo
R1V2 - getTextTrim: This is ValueTwo
Done
Start
R2V1 - getText: This is ValueOne
R2V1 - getTextTrim: This is ValueOne
R2V2 - getText: 
    This is ValueTwo
  
R2V2 - getTextTrim: This is ValueTwo
Done




More information about the jdom-interest mailing list