File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Outputstream string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Outputstream string" Watch "Outputstream string" New topic
Author

Outputstream string

Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
public static String beantoxml(Object bean){
byte currentXMLBytes[]
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("MyXML.xml")));
encoder.writeObject(bean);
encoder.close();
}

in this function instead of xml file i need the string directly, without creating the temp xml and reading from that


"Many of lifes failure are people who did not realize how close they were to success when they gave up."
-Thomas Edison
~ar~stutzen~
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

We are not a CodeMill and Show Some Effort

So what did you try?
What went wrong?
What don't you get?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
nothing went wrong.. wat i need is how to get the xml string from that encoder directly..
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

If I understood your question correctly, you should use a ByteArrayOutputStream instead of a FilteOutputStream.
After the object is encoded, you should call ByteArrayOutputStream.toString().

Hope it helps.

Regards,
Rok


SCJP, SCWCD
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
thanks rok.. thats wat i excepted .. i will try n post it
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
... or use the following:
new String( baos.toByteArray() );

The result should be the same ...


Rgds,
Rok
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
its not working.
The xml string is printing when i tested ByteArrayOutputStream.. Is my code is correct, sorry i am poor in stream concepts..

try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(baos));
encoder.writeObject(bean);
System.out.println("---------"+new String( baos.toByteArray() ));
encoder.close();
} catch (Exception e) {
e.printStackTrace();
}
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
sorry ranch i got, problem is with flush. when i added encoder flush it returned the string content.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Outputstream string