XML formatting LF/CR?

Discussion in 'Java' started by meselfo, Jan 9, 2007.

  1. meselfo

    meselfo Guest

    I cant get the java api to use linefeed and carriage return in the xml
    that im producing:

    StreamResult result = new StreamResult(output);
    DOMSource source = new DOMSource(doc);
    Transformer transformer =
    TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(source, result);

    The xml produced is a single line.
    What is needed to "pretty print" the xml?
     
    meselfo, Jan 9, 2007
    #1
    1. Advertising

  2. meselfo wrote:
    > I cant get the java api to use linefeed and carriage return in the xml
    > that im producing:
    >
    > StreamResult result = new StreamResult(output);
    > DOMSource source = new DOMSource(doc);
    > Transformer transformer =
    > TransformerFactory.newInstance().newTransformer();
    > transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    > transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    > transformer.transform(source, result);
    >
    > The xml produced is a single line.
    > What is needed to "pretty print" the xml?


    There are several ways of outputting an
    org.w3c.dom.Document nicely.

    But you can also do it in the XSLT transformation by
    putting:

    <xsl:text>
    </xsl:text>

    in the XSL source.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 10, 2007
    #2
    1. Advertising

  3. meselfo

    meselfo Guest

    Do you mean like this?:

    StringReader sr = new StringReader("<xsl:text></xsl:text>");
    javax.xml.transform.Source xsltSource =
    new javax.xml.transform.stream.StreamSource(sr);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(output);
    DOMSource source = new DOMSource(doc);
    TransformerFactory fac = TransformerFactory.newInstance();
    Transformer transformer = fac.newTransformer(xsltSource);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(source, result);

    It still results in single line xml.

    Btw. im using sun jdk 1.5.0_10


    Arne Vajhøj skrev:
    > meselfo wrote:
    > > I cant get the java api to use linefeed and carriage return in the xml
    > > that im producing:
    > >
    > > StreamResult result = new StreamResult(output);
    > > DOMSource source = new DOMSource(doc);
    > > Transformer transformer =
    > > TransformerFactory.newInstance().newTransformer();
    > > transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    > > transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    > > transformer.transform(source, result);
    > >
    > > The xml produced is a single line.
    > > What is needed to "pretty print" the xml?

    >
    > There are several ways of outputting an
    > org.w3c.dom.Document nicely.
    >
    > But you can also do it in the XSLT transformation by
    > putting:
    >
    > <xsl:text>
    > </xsl:text>
    >
    > in the XSL source.
    >
    > Arne
     
    meselfo, Jan 10, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Lookabaugh

    XML Formatting/Sorting Utility

    Mark Lookabaugh, Jul 17, 2003, in forum: XML
    Replies:
    1
    Views:
    3,421
    Chris Lovett
    Jul 17, 2003
  2. Slant
    Replies:
    1
    Views:
    407
    Slant
    Apr 30, 2004
  3. Replies:
    1
    Views:
    416
    Joseph Kesselman
    Jun 7, 2007
  4. cp
    Replies:
    2
    Views:
    419
  5. Erik Wasser
    Replies:
    5
    Views:
    515
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page