xstream icon indicating copy to clipboard operation
xstream copied to clipboard

Collection Objects Into Another Format For XML

Open Sheldon66-Huang opened this issue 3 years ago • 6 comments

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

Sheldon66-Huang avatar Jul 22 '22 10:07 Sheldon66-Huang

Hi, can you give an example?

joehni avatar Jul 23 '22 15:07 joehni

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

Sheldon66-Huang avatar Jul 26 '22 03:07 Sheldon66-Huang

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. :smile:

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

joehni avatar Jul 26 '22 17:07 joehni

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

Sheldon66-Huang avatar Jul 27 '22 01:07 Sheldon66-Huang

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!?!

joehni avatar Jul 28 '22 17:07 joehni

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

Sheldon66-Huang avatar Jul 29 '22 02:07 Sheldon66-Huang

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.

joehni avatar Aug 17 '22 17:08 joehni

Okay, thank you for your answer. Take a break

Sheldon66-Huang avatar Aug 19 '22 06:08 Sheldon66-Huang