Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Outputstream string

 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are not a CodeMill and Show Some Effort

So what did you try?
What went wrong?
What don't you get?
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nothing went wrong.. wat i need is how to get the xml string from that encoder directly..
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rok.. thats wat i excepted .. i will try n post it
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... or use the following:
new String( baos.toByteArray() );

The result should be the same ...


Rgds,
Rok
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ranch i got, problem is with flush. when i added encoder flush it returned the string content.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic