1

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/tutorial/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.

3
  • 1
    Is it necessary to convert this object to XML. Will JSON work ??? That will be Easy for u. Commented Sep 14, 2011 at 11:02
  • check your user's home directory - My Documents under windows, or ~username under unix. It's likely in there. Commented Sep 14, 2011 at 11:02
  • I'm trying Simple way of converting the object into xml. I will check if JSON is good for me. PD: I still can't find that st... xml!! Ag! It sound so silly! Commented Sep 14, 2011 at 11:59

3 Answers 3

5

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I saw it, and because of that I know I can do it. I will investigate further the Writer class.
That solution was the one I was looking for! Thank you soooo much!
3

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

Comments

1

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

result.getAbsolutePath()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.