• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Outputstream string

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nothing went wrong.. wat i need is how to get the xml string from that encoder directly..
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry ranch i got, problem is with flush. when i added encoder flush it returned the string content.
 
reply
    Bookmark Topic Watch Topic
  • New Topic