带有对象的Xml序列化器和反序列化器 [英] Xml Serializer and Deserializer with object

查看:64
本文介绍了带有对象的Xml序列化器和反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dot Net XmlSerializer中将对象转换为xml,然后在Deserializer中将xml标签转换为对象- 在Java中怎么做?

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)
  {
    ......
    ......
  }
}

我同意XStream,它将XML转换为对象,反之亦然,但是我的问题是.... 我必须用类调用converttoobject()并将其作为对象传递. 由于程序仍保留在C#中,因此我正在用Java执行相同的过程.

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是一个包含xml Root元素,属性和元素等的类. 在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:

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

现在您可以简单地运行String xml = xstream.toXML(joe);,结果是:

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>

要取回对象,请运行Person newJoe = (Person)xstream.fromXML(xml);

另一个可能的选择是来自 JAXB . org/wiki/Java_Architecture_for_XML_Binding"rel =" nofollow>维基百科:

Another possible option could be JAXB, from wikipedia:

用于XML绑定的Java体系结构 (JAXB)允许Java开发人员进行映射 Java类到XML表示形式. JAXB提供了两个主要功能: 将Java对象编组为 XML及其反义词,即解组 XML返回Java对象.其他 话说,JAXB允许存储和 以任何XML检索内存中的数据 格式,无需实施 一组特定的XML加载和 保存程序的例程 类结构.它类似于 .net中的xsd.exe和xmlserializers 框架.

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.

但是对我来说,大多数任务都可以通过XStream完成,XStream轻巧且更容易(恕我直言)

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

这篇关于带有对象的Xml序列化器和反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆