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 ?
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:
Now you can simple run
String xml = xstream.toXML(joe);
and the result is:To get object back run
Person newJoe = (Person)xstream.fromXML(xml);
Another possible option could be JAXB, from wikipedia:
But as for me, most tasks can be done with XStream, which lightweight and more easy (imho)
You could take a look at XStream.
In Java there's
XMLEncoder
Your methods will look something like:
(Note that
ReaderOutputStream
is from apache commons-io)