This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Outputstream string  RSS feed

 
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
 
Bartender
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..
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!