[jdom-interest] jdom 1.0 XMLOutputter

Bill Leng wleng at metatomix.com
Wed Sep 22 12:14:07 PDT 2004


Hi Bradley,

Thanks again for your help. However, I still have doubt. Folowing your 
thought, assume that we have a xml document and we parse it to a 
Document, we should be able to retrieve the exact text. This is ok for 
pre 1.0 jdom. But for jdom 1.0, we cannot do that (at least in a 
straight forward way). The Format.getRawFormat() is supposed to return a 
Format doing just that but it does not. I attached the simple test Java 
class. I am not sure if you can make it work.

thanks

Bill

Bradley S. Huffman wrote:

>Bill Leng writes:
>  
>
>>Thanks for your help but I still have some questions. Let me try to explain.
>>
>>1. the section 2.11 seems to be a requirement on xml processor to read 
>>input. when I use XMLOutputter to serialize a jdom, does this rule apply?
>>    
>>
>
>Yes and no, the spec. covers what the parser should do, must do, or can do,
>but not what object models like JDOM, XOM, DOM do.  But if a model is going to 
>faithfully represent a document then you would assume that if you serialize
>the model, then re-parse it you'd get back the same representation.
>
>  
>
>>2. even this rule apply, what about a single \n? it still gets 
>>translated to &#0A;
>>3. per XML spec, when "<root>\n</root>" gets parsed, \n does not need to 
>>be translated to &#0A;. Is this correct?
>>    
>>
>
>In element content it get translated to the current line seperator, which
>by default is "\r\n" and can be set in Format.
>
>  
>
>>4. how do you retrieve the text that contains \n unchanged?
>>    
>>
>
>Set the line seperator in Format to "\n".
>
>Brad
>  
>
-------------- next part --------------
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;

public class JdomTest2
{
  public static String[] testDocs = new String[]
  {
    "<r>aaa\nbbb</r>", // a single \n. nothing should happen
    "<r>aaa\r\nbbb</r>", // a sequence of \r\n, nothing should happen
    "<r>aaa\rbbb</r>" // a single \r, should be replaced with \n
  };

  public static void main(String[] args)
  {
    SAXBuilder builder = new SAXBuilder();
    try
    {
      int n = testDocs.length;
      for(int i = 0; i < n; i++)
      {
        ByteArrayInputStream stream = new ByteArrayInputStream(testDocs[i].getBytes());
        Document jdoc = builder.build(stream);
        XMLOutputter o = new XMLOutputter();
        Format f = Format.getRawFormat();
        f.setOmitDeclaration(true);
        o.setFormat(f);
        StringWriter w = new StringWriter();
        o.output(jdoc, w);
        String s = w.toString();
        //System.out.println("i="+i+" Output is ["+s+"]");
        boolean ok = s.equals(testDocs[i]);
        if(ok)
        {
          System.out.println("i="+i+" Output is ok");
        }
        else
        {
          System.out.println("i="+i+" Output is wrong. Got");
          int m = s.length();
          for(int j = 0; j < m; j++)
          {
            char c = s.charAt(j);
            if(c == '\r')
            {
              System.out.print('r');
            }
            else if(c == '\n')
            {
              System.out.print('n');
            }
            else
            {
              System.out.print(c);
            }
          }
          System.out.println();
        }
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

  }
}


More information about the jdom-interest mailing list