x-stream/xstream

Collection Objects Into Another Format For XML

Sheldon66-Huang opened this issue · 8 comments

I'd like to contribute some code about transforming collection objects into another format for XML

Hi, can you give an example?

Hi, can you give an example?

sorry,the last reply was by email,the format is not clear

I'll reply again

The following:

XStream can generate this style from the collection:

<class>
  <name>english</name>
  <students>
    <student>
      <name>jack</name>
      <age>16</age>
    </student>
    <student>
      <name>tom</name>
      <age>26</age>
    </student>
  </students>
</class>

however, this format cannot be generated:

<class>
  <name>english</name>
  <students>
    <student>
      <name>jack</name>
      <age>16</age>
    </student>
  </students>
  <students>
    <student>
      <name>tom</name>
      <age>26</age>
    </student>
  </students>
</class>

I implemented this format myself by inheriting Converter, so i wonder if i need to add this format conversion capability to XStream

Well, XStream can process this:

<class>
  <name>english</name>
  <student>
    <name>jack</name>
    <age>16</age>
  </student>
  <student>
    <name>tom</name>
    <age>26</age>
  </student>
</class>

Here the outer container is omitted and the elements of the collection are direct members of the type that keeps the collection. In contrast your format is quite unusual, because you repeat the container itself for each container element. Actually you're the first in the last 18 years to request such a structure. 😄

Therefore I would classify this as corner case, that does not necessarily have to be supported by the library out of the box.

You may attach your converter here to the issue. Maybe if there's more interest from other parties. They can then express their demand and I might reconsider this decision.

Nevertheless, thanks for your contribution and interest in XStream.

Regards,
Jörg

Well, XStream can process this:

<class>
  <name>english</name>
  <student>
    <name>jack</name>
    <age>16</age>
  </student>
  <student>
    <name>tom</name>
    <age>26</age>
  </student>
</class>

Here the outer container is omitted and the elements of the collection are direct members of the type that keeps the collection. In contrast your format is quite unusual, because you repeat the container itself for each container element. Actually you're the first in the last 18 years to request such a structure. 😄

Therefore I would classify this as corner case, that does not necessarily have to be supported by the library out of the box.

You may attach your converter here to the issue. Maybe if there's more interest from other parties. They can then express their demand and I might reconsider this decision.

Nevertheless, thanks for your contribution and interest in XStream.

Regards, Jörg

ok,I see, thank you for your answer, but I still have a question. Why can XStream recognize the format I mentioned and convert it into Object, but not process it

ok,I see, thank you for your answer, but I still have a question. Why can XStream recognize the format I mentioned and convert it into Object, but not process it

Sorry, I do not understand your question. If XStream converts a format into an object, it certainly has processed the XML!?!

ok,I see, thank you for your answer, but I still have a question. Why can XStream recognize the format I mentioned and convert it into Object, but not process it

Sorry, I do not understand your question. If XStream converts a format into an object, it certainly has processed the XML!?!

Let me show you what I want to say in my code
this is a xml

<class>
    <name>english</name>
    <students>
        <student>
            <name>jack</name>
            <age>16</age>
        </student>
    </students>
    <students>
        <student>
            <name>tom</name>
            <age>26</age>
        </student>
    </students>
</class>

and this is my code

SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("C:\\hwq\\IdeaProjects\\myproject\\src\\main\\resources\\xml\\class.xml"));

        XStream xStream = new XStream(new DomDriver());
        xStream.allowTypesByWildcard(new String[] {
                "com.my.test.xml.demo.**"
        });
        xStream.alias("class",Class.class);
        xStream.alias("student",Student.class);
        xStream.addImplicitCollection(Class.class,"students", List.class);

        Class myClass = (Class) xStream.fromXML(document.asXML());

        System.out.println(myClass);

It prints the result
Class{name='english', students=[[Student{name='jack', age='16'}], [Student{name='tom', age='26'}]]}

This format XStream can convert correctly

so I mean why can XStream go from XML to containers in this format

but it cannot go from a container to XML in this format

Sorry for the delay in answer, but I was busy. Real life happens.

I understand this now, but I can assure you, that its successful conversion is pure coincidence, since the reflection converter do not check if a member has already been read. Remember, XStream is "from Java to XML and back", i.e. in this scenario you would never have a member twice, because you cannot have that in Java.

Okay, thank you for your answer. Take a break