将XML转换为Object xstream(Android)

时间:2021-04-24 21:46:25

I'm looking for help with the conversion of an xml to object via XStream , here is my XML

我正在寻找通过XStream将xml转换为对象的帮助,这是我的XML

<main>
      <listDTO>
          <MyObject>
              <test>value1</test>
          </MyObject>
          <MyObject>
              <test>value2</test>
          </MyObject>

      </listDTO>
</main>

here are my classes.

这是我的课程。

public class First{
      MyObject[] listDTO;
}

public class MyObject{
      String test;
}

With xstream :

使用xstream:

...
XStream xStream = new XStream();
xStream.alias("main",First.class);
xStream.alias("listDTO", MyObject.class);
xStream.addImplicitCollection(First.class,"listDTO");

....

The tag <listDTO> is a problem, and I can not change the XML . The classes were generated from wsdl with Eclipse .

标签 是个问题,我无法更改XML。这些类是使用Eclipse从wsdl生成的。

Can you help me ?

你可以帮我吗 ?

1 个解决方案

#1


0  

Your code should look like this:

您的代码应如下所示:

XStream xStream = new XStream();
xStream.alias("main", First.class);
xStream.alias("MyObject", MyObject.class);

First, you don't have implicit collection, but explicit one marked by listDTO tag. With implicit collection your XML would be:

首先,您没有隐式集合,而是由listDTO标记标记的显式集合。使用隐式集合,您的XML将是:

<main>
    <MyObject>
        <test>value1</test>
    </MyObject>
    <MyObject>
        <test>value2</test>
    </MyObject>
</main>

And second error you have made was adding listDTO alias for MyObject class. That should be replaced with MyObject alias, since you do have MyObject tag defined in your XML that corresponds to MyObject class.

您所做的第二个错误是为MyObject类添加listDTO别名。应该用MyObject别名替换它,因为您在XML中定义了与MyObject类对应的MyObject标记。

#1


0  

Your code should look like this:

您的代码应如下所示:

XStream xStream = new XStream();
xStream.alias("main", First.class);
xStream.alias("MyObject", MyObject.class);

First, you don't have implicit collection, but explicit one marked by listDTO tag. With implicit collection your XML would be:

首先,您没有隐式集合,而是由listDTO标记标记的显式集合。使用隐式集合,您的XML将是:

<main>
    <MyObject>
        <test>value1</test>
    </MyObject>
    <MyObject>
        <test>value2</test>
    </MyObject>
</main>

And second error you have made was adding listDTO alias for MyObject class. That should be replaced with MyObject alias, since you do have MyObject tag defined in your XML that corresponds to MyObject class.

您所做的第二个错误是为MyObject类添加listDTO别名。应该用MyObject别名替换它,因为您在XML中定义了与MyObject类对应的MyObject标记。