Converting XML into fomatted string

Hi All,

I need to convert a DOM Object into a String Object . I am able to do so by using

the following code :

TransformerFactory tfac = TransformerFactory.newInstance();

Transformer xformer = tfac.newTransformer();

Transformer xformer = tfac.newTransformer();

xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

xformer.setOutputProperty(OutputKeys.METHOD, "xml");

xformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");

xformer.setOutputProperty(OutputKeys.INDENT,"yes");

xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

xformer.transform(new DOMSource(document), strResult);

Stringresult = strResult.getWriter().toString();

But the returned String object is not formatted , Is there any property or method which i can use to get Formatted output ?

Thanks in advance !!!

[907 byte] By [gauravgupta_ina] at [2007-10-2 6:18:25]
# 1
he how do u create the Result object which transformer needs ?Which is the class that implements Result interface ?
Nikhil_Bhavsara at 2007-7-16 13:20:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hi , > xformer.transform(new DOMSource(document), strResult);> Stringresult = strResult.getWriter().toString();What is StrResult ? If it is an Object of type DomResult then it dont have method getWriter() ? pla help me out !
Nikhil_Bhavsara at 2007-7-16 13:20:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

First, I think you need to get the JavaDoc Documetation for the level of SDK you are using, and learn how to use it.

The Transformer object's transform method takes a "Source" and a "Result" arguement. When you look up "Result" you see that a Result is an interface, and that three classes implement it -- DOMResult, SAXResult, and StreamResult. You use DOMResult if you want the transformer to build a DOM Tree of the output, SAXResult if you want to feed the output through SAX, and StreamResult if you want to send the output (unprocessed) to a file, or to another consumer of that file.

You can also see that StreamResult has a getWriter method.

I would also suggest that you download Eclipse from www.eclipse.org and learn to use it. With Eclipse, you can see the JavaDoc content just by placing the mouse over a variable or Class name. And, like the JavaDocs from Sun, it is free.

Dave Patterson

d.pattersona at 2007-7-16 13:20:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

> The Transformer object's transform method takes a

> "Source" and a "Result" arguement. When you look up

> "Result" you see that a Result is an interface, and

> that three classes implement it -- DOMResult,

> SAXResult, and StreamResult. >

Ya .. later i found it ...

But one question , i want to Transform a DOM object in to a String ..

I want output as string not as file .....

So suggest some code .. ?

I would also suggest that you download Eclipse from

> www.eclipse.org and learn to use it. With Eclipse,

> you can see the JavaDoc content just by placing the

> mouse over a variable or Class name. And, like the

> JavaDocs from Sun, it is free.

I have already worked on it ...

And right now , i m using WebSphere Application Studio Developer : ) -An extension of Eclipse ...

Nikhil_Bhavsara at 2007-7-16 13:20:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
One of the constructors for a StreamResult takes a Writer as a parm.A StringWriter is one kind of Writer that can be used to create a String as the result of a transform.Dave Patterson
d.pattersona at 2007-7-16 13:20:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Thnk u very muh !! I got my solution ...
Nikhil_Bhavsara at 2007-7-16 13:20:20 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...