I am trying to create a simple XML in string format but I cannot seem to get Eclipse to play along as it keeps giving me a "cannot convert from void to String" error warning for this code (I followed along this relatively recent post on Stackoverflow about how to add a tag for an xml version.) Here is my example code:
final Xstream xstream = new Xstream();
final Person myPerson = new Person();
Writer writer = new StringWriter();
try {
writer.write("<?xml version=\"1.0\"?>");
} catch (final IOException e) {
e.printStackTrace();
System.exit(0); //just for testing sakes
}
final String xmlResponse = xstream.toXML(myPerson, writer); // the line that has the error in Eclipse
For the obvious answer of "Maybe you aren't actually making an object for myPerson," the code I have for Person is:
public class Person{
public final String name;
Person(){
name = "EmptyName";
}
}
I have searched the documentation and have found nothing about this error that would help. However, there is in the source for Xstream the method void toxml(Xstream, Object, Writer)
which could potentially be the issue. The other thing it might be is escaping the quotation marks in the string could also be the issue, but I do not have a way around that. Plus removing the quotations and escapes doesn't fix the issue.
Any help in solving this would be appreciated. Xstream version 1.4.10 in case that matters.
If you look at the documentation for toXML
, it returns void
, means there is no return type and hence you see that error in Eclipse.
You can simply write:
xstream.toXML( myPerson, writer );
Remove String xmlResponse =
and then you can assign the xml to xmlResponse
using :
final String xmlResponse = ( (StringWriter)writer ).getBuffer().toString();