Search code examples
javajakarta-eexstream

XStream - Tag Creation for xml version


I was going through the Xstream tutorials

http://x-stream.github.io/annotations-tutorial.html

How can i add the processing instruction to the xml responses

<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n

Ex: Xstream gives the response as

<message><type>15</type></message>

But i would want the instruction also part of the response..

Is there any way i could get it ..

For now am prefixing the string response with this tag which i feel is not the best approach.

Regards


Solution

  • If you are talking about adding the XML header to the output, here's how to do that:

    XStream xstream = new XStream();
    Writer writer = new StringWriter();
    writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
    xstream.toXML(object, writer);
    System.out.println(writer.toString());