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 !!!
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
> 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 ...