Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can it be possible to write xml doc into a string variable?

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam using ajax for my application, i have to create XMLHttpResponse which is an xml

tell me how can i send xml response using stax?
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Abishek,

Do you want to create an xml response on the server side for a request made through and XMLHttpRequest? Then I hope this helps..

Check here for a tutorial on StAX. By clicking the link "Next Page" you can navigate to the next page where you will find instructions on how to create an xml document using the StAX API.
As the article points out, the well formedness of documents that you create through this method is not guaranteed. The user has to take care that the document is well formed.

However it could save you a lot of work creating your xml documents this way.

You could use this in few different ways..

You could pass an OutputStreamWriter to XMLOutputFactory.createXMLStreamWriter(java.io.Writer) and use the returned XMLStreamWriter to write to the writer / stream.

Or use a StringWriter and get the string after the writing is done and use it as you wish.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic