Xml Serializer and Deserializer with object

2019-08-13 23:12发布

问题:

In Dot Net XmlSerializer objects to xml and Deserializer to convert xml tags to objects -- how to do in java ?

public static object ConvertToObject(Type objectType,String strxml)
{
  try
  {
    Object obj=null;
    XmlSerializer xs=new XmlSerializer(objectType);
    obj=xs.Deserializer(new StringReader(strxml));
    return obj;
  }
  catch(Exception e)
  {
  }

//Xml serialized with ihuti java and converted to xml by argobj 
public static object ConvertFromObject(ihutidata argobj)//ihuti.java with xml elements
{
  try
  {
    XmlWriterSettings Sett=new XmlWritterSettings();
    settings.OmitXmlDeclaration=true;

    StringBuilder builder=new StringBuilder();
    XmlSerializer xs=new XmlSerializer(typeof(ihutidata));
    XmlWriter xw=XmlWriter.Create(builder,settings);

    xs.Serialize(xw,argobj);
    xw.close();

    return (builder.ToString());
  }
  catch(Exception e)
  {
    return("Exception"+e);
  }
internal string savetodb(TSring argdata,string argclientip)
{
  ihutidata req=(ihutidata)DataProcessing.ConvertToObject(typeof(ihutidata),argdata);

  if(req!=null)
  {
    ......
    ......
  }
}

ya I agree with XStream it convert the xml to object and vice versa but my problem is.... i have to call converttoobject() with class and pass it as object. As the program remains in C# i am doing same process in java.

ihutidata is an class which contains xml Root elements, attributes and elements etc. Is it Possible in Java ?

回答1:

There are many libraries to do this job (serialize and deserialize objects), one of the simplest in use is XStream, here is example of using:

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

Now you can simple run String xml = xstream.toXML(joe); and the result is:

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

To get object back run Person newJoe = (Person)xstream.fromXML(xml);

Another possible option could be JAXB, from wikipedia:

Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects. In other words, JAXB allows storing and retrieving data in memory in any XML format, without the need to implement a specific set of XML loading and saving routines for the program's class structure. It is similar to xsd.exe and xmlserializers in .Net Framework.

But as for me, most tasks can be done with XStream, which lightweight and more easy (imho)



回答2:

You could take a look at XStream.



回答3:

In Java there's XMLEncoder

Your methods will look something like:

public static <T> T convertToObject(Class<T> objectType, String xml) {
    XMLDecoder decoder = new XMLDecoder(new ReaderInputStream(new StringReader(xml)));
    T result = (T) decoder.readObject();
    decoder.close();
    return result;
}

public static String convertToString(Object object) {
    StringWriter sw = new StringWriter();
    XMLEncoder encoder = new XMLEncoder(new WriterOutputStream(sw));
    encoder.writeObject(object);
    encoder.close();
    return sw.toString();
}

(Note that ReaderOutputStream is from apache commons-io)