开发者

Problems converting from an object to XML in java

What I'm trying to do is to convert an object to xml, then use a String to transfer it via Web Service so another platform (.Net in this case) can read the xml and then deparse it into the same object. I've been reading this article:

http://simple.sourceforge.net/download/stream/doc/tutori开发者_开发百科al/tutorial.php#start

And I've been able to do everything with no problems until here:

Serializer serializer = new Persister();
PacienteObj pac = new PacienteObj();
pac.idPaciente = "1";
pac.nomPaciente = "Sonia";
File result = new File("example.xml");
serializer.write(pac, result);

I know this will sound silly, but I can't find where Java creates the new File("example.xml"); so I can check the information.

And I wanna know if is there any way to convert that xml into a String instead of a File, because that's what I need exactly. I can't find that information at the article.

Thanks in advance.


And I wanna know if is there any way to convert that xml into a String instead of a File, because that's what I need exactly. I can't find that information at the article.

Check out the JavaDoc. There is a method that writes to a Writer, so you can hook it up to a StringWriter (which writes into a String):

 StringWriter result = new StringWriter(expectedLength);
 serializer.write(pac, result)
 String s = result.toString();


You can use an instance of StringWriter:

Serializer serializer = new Persister();
PacienteObj pac = new PacienteObj();
pac.idPaciente = "1";
pac.nomPaciente = "Sonia";

StringWriter result = new StringWriter();
serializer.write(pac, result);
String xml = result.toString();  // xml now contains the serialized data


Log or print the below statement will tell you where the file is on the file system.

result.getAbsolutePath()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜