[jdom-interest] Problems with JDOMSource and Xalan

Linus Birgerstam linus.birgerstam at lorensbergs.com
Tue Apr 9 00:44:04 PDT 2002


Hello

I'm having some problems transforming a JDOM Document to HTML. I'm not sure if it's a JDOM problem or a Xalan problem but I would really appreciate your help.

I'm trying to use a JDOMSource as input to the Xalan transformer. The problem is that the transformation inserts an extra CR (
) into my HTML document.

If I use Code 1 to transform a document containing the string
"line1
line2"

The result is
"line1
line2"

However, if I use an XMLOutputter as in Code 2 the result comes out just the way I want it, does anyone know why this is?


/Linus


/* Code 1 */
private byte[] toHTML(Document xml, Transformer xsl) throws Exception {
    JDOMSource source = new JDOMSource(xml);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(out);

    xsl.transform(source, result);
    byte[] data = out.toByteArray();
    return data;
}

/* Code 2 */
private byte[] toHTML(Document xml, Transformer xsl) throws Exception {
    XMLOutputter xmlOut = new XMLOutputter("   ",true,"ISO-8859-1");
    ByteArrayOutputStream xout = new ByteArrayOutputStream();
    xmlOut.output(xml, xout);
    byte[] xdata = out.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(xdata);
    StreamSource source = new StreamSource(in);

    ByteArrayOutputStream hout = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(hout);

    xsl.transform(source, result);
    byte[] data = hout.toByteArray();
    return data;
}


------------------------------------------------------------------------
Linus Birgerstam                 Email: linus.birgerstam at lorensbergs.com
Lorensbergs Communication AB     URL:   http://www.lorensbergs.com
Drottningatan 31                 Phone: 031-133009
411 14 Göteborg                  Fax:   031-133019 
 



More information about the jdom-interest mailing list