1

What I'm trying to do is to parse an object into a String, and then , parse it into an XML so any other language can translate it.

Figure out this object:

public class DatosPac 
{
private String nombre;
private String apellidos;
private String dni;
public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public String getApellidos() {
    return apellidos;
}
public void setApellidos(String apellidos) {
    this.apellidos = apellidos;
}
public String getDni() {
    return dni;
}
public void setDni(String dni) {
    this.dni = dni;
}   
}

What I want to do is, parse it into a common XML between Android and .Net so both languages can translate the same object. The way to communicate both languages will be using Web Services, so the Web Service will receive a String, transalte it into the object and then use the information. Bidirectionally. I mean, Android will be able to receive an object parsed from .Net, and .Net will be able to receive the same object from Android. To be able to do this, I think I need to convert them into the same XML, but I don't know how to do it in Android.

Thanks in advance.

2 Answers 2

2

There are several XML serializing and de-serializing libraries available for Android. And I am sure the same's the case with .NET.

You set up your objects as POJOs and with a few annotations, you can serialize/deserialize in a few lines of code. In the Android world, I personally prefer Simple, but there are various other libraries available.

A more compact, (and more efficient, in terms of parsing) data representation format is JSON. There are multiple libraries available for parsing and constructing JSON too. My preferred one for Android is Gson.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to serialize the String to XML in Android. I know how to do the same in .Net, but I'm not pretty sure how to do it using Android. What do you recommend me??? The Simple library? Can you post a short example on how to serialize and deserialize???? thank you!!
The website I pointed you to contains an example which is very similar to what you are trying to achieve. Go ahead - check it out - you can get started in 5 minutes.
Oh, ok, I didn't realize. Thank you!! :)
I still have some problems while trying to convert from an object to an XML. I've done until here: Serializer serializer = new Persister(); PacienteObj pac = new PacienteObj(); pac.nombre = "Sonia"; File result = new File("example.xml"); serializer.write(pac, result); It seems you save into a file the XML we're talking about. But two problems: 1. I can't finde where it creates the new File("example.xml"); I know it sounds silly, but I still can't find it. 2. What I want is the object to be serialized into a String, instead of a File object. Is there any direct or easy way?? Thanks!
Of course you can serialize into String. Serializer serializer = new Persister(); StringWriter writer = new StringWriter(); try { serializer.write(pac, writer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String serializedXml = writer.toString();
0

EDIT: I believe I was a bit too quick! I didn't notice the android tag and assumed a .net context. Still, one bit stands: You probably want to serialize, not to "parse" the object, into XML.

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.