I read a mail similar to yours a little while back and sort of had to solve similar problems. I have problems sending the mail out, which is why it took until now.
Anyway, another way to go around this problem is to use the special attribute "xml:space". The only legal values for xml:space are "preserve" and "default". The value default indicates that the default processing is desired, which is not to preserve space. And org.apache.xml.serialize.XMLSerializer in Xerces-2.1 seems to respect this attribute. Thus, in your example, if you had added 'xml:space="preserve"' to your element <string> when you are constructing your DOM tree (for example), you would get something like below: <?xml version="1.0" encoding="UTF-8"?> <content> <string xml:space="preserve">I love you</string> </content> HTH, Han Ming > >> Fellows! >> >> I have a DOM tree that I create programmatically. Subsequently I >> use org.apache.xml.serialize.XMLSerializer to write out the XML. I >> would like to preserve all the different kind of whitespace in the >> XML document; so I use setPreserveSpace() in >> org.apache.xml.serialize.OutputFormat. >> >> It works well when the identation is off. Here's some output: >> >> <?xml version="1.0" encoding="UTF-8"?> >> <content><string id="11">I love you</string></content> >> >> (note that there is a couple of tabs in the string) >> >> But when I turn the identation on, the output is ugly: line breaks >> are introduced at seemingly random places in the elements, e.g. >> between 2 attributes. The string content is correctly output though: >> >> <?xml version="1.0" encoding="UTF-8"?> >> <content><string >> id="11">I love you</string></content> >> >> >> What I would like to see is >> >> <?xml version="1.0" encoding="UTF-8"?> >> <content> >> <string id="11">I love you</string> >> </content> >> >> Is there some parameter that I am missing? Or is it a bug that I >> should report? >> >> Thanks for any advice! >> >> Ohmson > On Monday, October 7, 2002, at 03:08 PM, [EMAIL PROTECTED] wrote: > Hi, > I'm using Castor from Exolab group which use Xerces. I have problem > in > preserving line breaks in text content while settng indent for xml > output. > Here is the > code: > > OutputFormat format = new OutputFormat(); > format.setMethod(Method.XML); > format.setPreserveSpace(true); > format.setIndenting( true ); > format.setIndent( 2 ); // 2-space indention > format.setLineWidth( 16384 ); // As large as needed to prevent > linebreaks in text nodes > XMLSerializer serializer = new XMLSerializer( new > FileWriter(OutFileName), format); > > This code, while preserving line breaks in text content in xml, > doesn't > set indent > properly. If I remove format.setPerserveSpace(true), it sets indent > properly but the line > breaks in text content are not retained. Does anyone have the same > problem? > Thanks in advance. > > Ho-chun > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
