If you want pretty printing then thats the way to do it. You can use a
StringWriter instead which is a bit faster.

OutputFormat outFormat = OutputFormat.createPrettyPrint();
outFormat.setEncoding("ISO-8859-1");

StringWriter writer = new StringWriter();
XMLWriter out = new XMLWriter(writer, outFormat);
out.write(newDoc);
String s = writer.toString();
System.out.println("OUT: " + s);


Of course if what you really want to do is output it to a file or System.out
then you can do that even easier...

OutputFormat outFormat = OutputFormat.createPrettyPrint();
outFormat.setEncoding("ISO-8859-1");

XMLWriter out = new XMLWriter(System.out, outFormat);
out.write(newDoc);

James
----- Original Message -----
From: "Christian Holmqvist, IT, Posten" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, April 24, 2002 10:15 AM
Subject: RE: [dom4j-user] Formatting of 'asXML()' result


Hi

If you have the same problem as I have and want to get your XML code as a
String (if your logging system only handles String logging, like mine, for
example) I made up a little trick that looks like this.

(newDoc is a Document)

      OutputFormat outFormat = OutputFormat.createPrettyPrint();
      outFormat.setEncoding("ISO-8859-1");
      ByteArrayOutputStream bao = new ByteArrayOutputStream();
      XMLWriter out = new XMLWriter(bao, outFormat);
out.write(newDoc);
String s = bao.toString();
      System.out.println("OUT: " + s);

Cheers Christian
P.s. There might be a easier way to get the Document nicely printed into a
String but I could not find it.... D.s.



> -----Ursprungligt meddelande-----
> Fr�n: Joshua Paech [mailto:[EMAIL PROTECTED]]
> Skickat: den 24 april 2002 03:17
> Till: [EMAIL PROTECTED]
> �mne: [dom4j-user] Formatting of 'asXML()' result
>
>
> I want to print out particular dom4j Elements containing a
> few attributes
> plus some text. If I do an element.asXML(), the String
> returned is all one
> line. Is it possible to get newlines inserted between each attribute?
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user
>

_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to