Subjects
Home
Xalan extension functions
Fomatting question serializing DOM with pretty print
xalan with pull parser
Cannot find the declaration
Apache Xalan drop support to run on JRE 1 1 x
Why does Doctype change processing of a document
Node set to XML string via Java extensions in Xalan J: possible?
Templates/Transformers + thread safety???
Problem evaluating xpath with muliple prefix with different namespace
remove an arbitrary attribute from xsl output
Xalan3 XSLT 2 0 XPath 2 0 support?
Problem using compiled translets with Xalan !!
Xalan and jstl 1 1 problem with transform tag
NullPointer in DOM2DTM getLocalName
URIResolvers base parameter with xsltc and cascaded imports
Performance problem for Xalan J on intel dual core
Standard libraries in JAXP?
Serializing a DOM tree to XML file, customize entities replacement
Library Conflict Involving BCEL Library
A question on how users are using <xsl:message >
Kevin Cormier as a new Apache Xalan J committer
Struggling to iterate over tokenized string
Xalan count() trouble
Problem with recursive xpath
Error when switching to java 1 5
document( ' ')
Problem with Xalan2 7 0 transformation
cr/lf options
entity encoded XML
can xalan transform 2 xml using one xslt?
Xalan J JIRA defect review Monday October 16, 2006 from 2:00 to 3:30 pm ED
xsl transform with cdata section elements
xslt parameters not expanded
Weird behavior of XPath evaluate()
How to avoid <xsl:message > instruction prints stylesheet file informations ?
Cannot find SimpleTransform subdirectory after installing Xalan J
recover from document not found exceptions
jdk1 5 and Xalan jar differences?
Performance Issue
Error/Bug adding floating point numbers
XPathAPI: eval exp using nodes with default namespace
modifying xalan to output invalid XML
NullPointerException
mege two separate xml nodes into one
Is this a XALAN document identification bug?
is StylesheetRoot really java io Serializable ?
transform() fails for DOMSource but succeeds for StreamSource
Thoughts on Transformer parameter passing
HELP, Xalan and jstl 1 1 problem with transformer
Problem with XPath namespace axis?
string utils:replace deleting search string if replacement string is an HTML
help with enumeration values pls
xalan 2 5 1 vs 2 7 performance question
How to insert/update in XML document
HTML Serialization and Handling of Ampersands in HREF Attributes
XHTML link tag stripping
SystemId Unknown; Line #24; Column #49; java lang NullPointerException
xpath text() help
Apostrophe problem with xalan 2 7 0
How to set variables in XML document?
Links
Home
Oracle database error code ...
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
Serializing a DOM tree to XML file, customize entities replacement

Serializing a DOM tree to XML file, customize entities replacement

2007-06-08       - By Jean-Francois Beaulac
Reply:     1     2     3     4     5     6  

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
*
*/

/**
*
* @(protected) 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
    &lt; 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()
+ "@(protected)");
           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:minchau@(protected)]
Sent: June 8, 2007 4:53 PM
To: Jean-Francois Beaulac
Cc: xalan-j-users@(protected)
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 &#13; 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:        minchau@(protected)



                                                                         
            Jean-Francois                                                
            Beaulac                                                      
            <jean-francois.be                                          To
            aulac@(protected)         Brian Minchau/Toronto/IBM@(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 &#13; 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:minchau@(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:        minchau@(protected)




            Jean-Francois
            Beaulac
            <jean-francois.be                                          To
            aulac@(protected)         xalan-j-users@(protected)
            >                                                          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 &#13;. 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 &#13; 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:

- &lt;
- &gt;
- &amp;
- &apos;
- &quot;

Thank you

================================
Jean-Francois Beaulac
jean-francois.beaulac@(protected)