[jdom-interest] Bug in CDATA reading/writing?
Mark Roder
roder at is.com
Thu Mar 22 05:58:55 PST 2001
Thanks for checking in the new XMLOutputter.java(CVS rev 1.41)
I am noticing a problem that you/someone may be able to track down better
than I can.
If I construct a outputter with:
XMLOutputter xmlOut = new XMLOutputter(" ",true);
And read and then write out a file that contains:
<Root>
<ValueOne>This & That</ValueOne>
<ValueTwo>This <![CDATA[&]]> That</ValueTwo>
<LongElem><Tag1>T1</Tag1><Tag2><Tag21>T21</Tag21></Tag2><Tag3>T3</Tag3></Lon
gElem>
</Root>
The ValueOne, ValueTwo and LongElem are not indented, but the line before
them has 2 spaces on it and a newline. Notice it goes
<Root>\r\n<space><space>\n<ValueOne>.
Here is the od -c dump of the the original file and then the file I write
out. I do get the same output if I have the original file with line endings
of \r\n or just \n.
Thanks for the help.
Mark
bash-2.02$ od -c Sample.xml
0000000 < R o o t > \r \n < V a l u e O n
0000020 e > T h i s & a m p ; T h a
0000040 t < / V a l u e O n e > \r \n < V
0000060 a l u e T w o > T h i s < ! [
0000100 C D A T A [ & ] ] > T h a t <
0000120 / V a l u e T w o > \r \n < L o n
0000140 g E l e m > < T a g 1 > T 1 < /
0000160 T a g 1 > < T a g 2 > < T a g 2
0000200 1 > T 2 1 < / T a g 2 1 > < / T
0000220 a g 2 > < T a g 3 > T 3 < / T a
0000240 g 3 > < / L o n g E l e m > \r \n
0000260 < / R o o t > \r \n
0000271
bash-2.02$ od -c Sample2.xml
0000000 < ? x m l v e r s i o n = " 1
0000020 . 0 " e n c o d i n g = " U T
0000040 F - 8 " ? > \r \n < R o o t > \r \n
0000060 \n < V a l u e O n e > T h i
0000100 s & a m p ; T h a t < / V a
0000120 l u e O n e > \r \n \n < V a l
0000140 u e T w o > T h i s < ! [ C D
0000160 A T A [ & ] ] > T h a t < / V
0000200 a l u e T w o > \r \n \n < L o
0000220 n g E l e m > \r \n < T a
0000240 g 1 > T 1 < / T a g 1 > \r \n
0000260 < T a g 2 > \r \n
0000300 < T a g 2 1 > T 2 1 < / T a g 2
0000320 1 > \r \n < / T a g 2 > \r
0000340 \n < T a g 3 > T 3 < / T
0000360 a g 3 > \r \n < / L o n g E l
0000400 e m > \r \n \n \r \n < / R o o t
0000420 > \r \n \r \n
0000425
bash-2.02$
Here is the code I use to read a file and then write the new file.
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
public class DataReader
{
public static void main(String args[])
{
try
{
SAXBuilder aBuilderR1 = new SAXBuilder();
Document aDocR1 = aBuilderR1.build(new File("Sample.xml"));
XMLOutputter xmlOut = new XMLOutputter(" ",true);
FileWriter aWriter = new FileWriter("Sample2.xml");
xmlOut.output(aDocR1,aWriter);
aWriter.close();
}
catch(Exception e)
{
System.out.println("Exception is " + e);
}
}
}
More information about the jdom-interest
mailing list