Hi, I made a test program which outputs a < , a \n and a System.getProperty("line.separator").
The simple \n comes out fine, the only problem I have is with the System.getProperty("line.separator"). Thank you import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; /* * Test.java * * Created on June 8, 2007, 4:55 PM * */ /** * * @author Jean-Francois Beaulac */ public class Test { public static void main(String args []){ try{ // Generate a DOM tree /* <ROOT> <TEXT> Test text < With special character </TEXT> </ROOT> Should result in: <ROOT><TEXT>Test text < With special character after line.separator.</TEXT></ROOT> */ DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); Document doc = docBuilder.newDocument(); //<QBXML> Element root = doc.createElement("ROOT"); doc.appendChild(root); Element text = doc.createElement("TEXT"); root.appendChild(text); text.appendChild(doc.createTextNode("Test text\n< With special character" + System.getProperty("line.separator") + "after line.separator.")); // Transformation TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.METHOD,"xml"); //create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); // Console System.out.println(sw.toString()); // File java.io.File file = new java.io.File(System.currentTimeMillis() + "@lastRequest.xml"); java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(file, true)); writer.write(sw.toString()); writer.flush(); writer.close(); }catch(Exception e){ } } } -----Original Message----- From: Brian Minchau [mailto:[EMAIL PROTECTED] Sent: June 8, 2007 4:53 PM To: Jean-Francois Beaulac Cc: xalan-j-users@xml.apache.org Subject: RE: Serializing a DOM tree to XML file, customize entities replacement HI Jean-Francois, please post a small Java program that creates a small DOM, for example a document with only a root element, that has a text node child with say a '>' and a '\n' in it. Also your code to serializer the DOM so I can see how the comes about. I'm willing to investigate, but I'm not willing to spend time trying to create the testcase. Thanks, - Brian - - - - - - - - - - - - - - - - - - - - Brian Minchau, Ph.D. XSLT Development, IBM Toronto e-mail: [EMAIL PROTECTED] Jean-Francois Beaulac <jean-francois.be To [EMAIL PROTECTED] Brian Minchau/Toronto/[EMAIL PROTECTED] > cc 06/08/2007 04:43 Subject PM RE: Serializing a DOM tree to XML file, customize entities replacement Hi, Is that option supposed to change the String the transformer will use to replace line separators? I just tried it and it changes nothing at all, my XML output is still filled with strings. What I am looking for would be a way to disable output escaping for everything, except the characters I listed in my first post. If I add the processing instruction in my DOM using: ProcessingInstruction pi = doc.createProcessingInstruction(Result.PI_DISABLE_OUTPUT_ESCAPING, ""); root.getParentNode().insertBefore(pi, root); I get the desired result, but then I would need to manually escape all the < > & ' " characters. Thank you -----Original Message----- From: Brian Minchau [mailto:[EMAIL PROTECTED] Sent: June 8, 2007 4:16 PM To: Jean-Francois Beaulac Subject: Re: Serializing a DOM tree to XML file, customize entities replacement Hi Jean-Francois, I think there are solutions to this, but all of them are Xalan specific. I assume that you are running your DOM through the identity transformation in order to serialize it. This is the most portable way to do it. Once you get your Transformer object, even though it is the identity transform, you can set some properties via JAXP. I suggest you try this: javax.xml.transform.Transformer t = ... t.setOutputProperty("{http://xml.apache.org/xalan}line-separator"," "); If you had a stylesheet this could be done like this: <xsl:out xalanPrfx:line-separator=" " xmlns:xalanPrfx="http://xml.apache.org/xalan" /> but you don't have a stylesheet. Still JAXP lets you over-ride xsl:output attribute values, and I think this should work even when there is no stylesheet. So my suggestion is to not output the '\n' but to output a space. Of course if you want something else like "-EndOfLine-" then do this: t.setOutputProperty("{http://xml.apache.org/xalan}line-separator","-EndOfLin e-"); Hope this does the job for you. - Brian - - - - - - - - - - - - - - - - - - - - Brian Minchau, Ph.D. XSLT Development, IBM Toronto e-mail: [EMAIL PROTECTED] Jean-Francois Beaulac <jean-francois.be To [EMAIL PROTECTED] xalan-j-users@xml.apache.org > cc 06/08/2007 03:01 Subject PM Serializing a DOM tree to XML file, customize entities replacement Hi, I am currently building a DOM tree using the Xerces implementation and then write it to a String using the Xalan transformer. I currently have a problem with line breaks (I use System.getProperty("line.separator")) in the text nodes being replaced by the entity . The application I am trying to then send the XML message to does not transform that entity back into a line break. Is there a way to tell Xalan to use either a custom set of entities, or to remove specific entities from this automatic treatment or am I force the reparse manually the result to replace the back to a normal line separator. Having a way to tell the transformer to use a custom set of entities would be my best option since the application I communicate with only threats: - < - > - & - ' - " Thank you ================================ Jean-Francois Beaulac [EMAIL PROTECTED]
Test.java
Description: Binary data