[xstream-user] Deserialization with "value="

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[xstream-user] Deserialization with "value="

Holger-3
How can I get data from an XML like this one below?

<InHouse>
  <MessageTyp value="DELFOR">
  <DocNo value="53442">
</InHouse>

By deserializing with the following statements an error occurs

  XStream xs = new XStream(new DomDriver());
  xs.alias("InHouse",InHouse.class);
  String XMLStr = logMemo.getText();
  InHouse NewIH = (InHouse)xs.fromXML(XMLStr);

testing by

  logMemo.append(xs.toXML(NewAFPS));

I thought to solve the problem by using the PropertyConverter

  xs.registerConverter(new PropertiesConverter());

The equal isn't different. What have I done wrong here?

Reply | Threaded
Open this post in threaded view
|

[xstream-user] Re: Deserialization with "value="

Mauro Talevi
Holger wrote:

> How can I get data from an XML like this one below?
>
> <InHouse>
>   <MessageTyp value="DELFOR">
>   <DocNo value="53442">
> </InHouse>
>
> By deserializing with the following statements an error occurs
>
>   XStream xs = new XStream(new DomDriver());
>   xs.alias("InHouse",InHouse.class);
>   String XMLStr = logMemo.getText();
>   InHouse NewIH = (InHouse)xs.fromXML(XMLStr);
>
> testing by
>
>   logMemo.append(xs.toXML(NewAFPS));
>
> I thought to solve the problem by using the PropertyConverter
>
>   xs.registerConverter(new PropertiesConverter());
>
> The equal isn't different. What have I done wrong here?
>
The PropertiesConverter expects two attributes, name and value,
and serialises Properties objects

For your use case, you'd probably want to define your own converter
and register it.

Something like the following:

     class MyObjectConverter implements Converter {

         public boolean canConvert(Class type) {
             return type == MyObjectConverter.class;
         }

         public void marshal(Object source, HierarchicalStreamWriter
writer, MarshallingContext context) {
             MyObject obj = (MyObject) source;
             writer.addAttribute("value", obj.getValue());
         }

         public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext context) {
             String value = reader.getAttribute("value");
             return new MyObject(value);
         }
     }

Cheers

Reply | Threaded
Open this post in threaded view
|

Re: [xstream-user] Re: Deserialization with "value="

Door.Xiao
Good Idea. I met the other problem on the XML file outputing.
 
There are two model classes:
public class JavaObjectModel {
    private String name;
    private String classname;
    private String infile;
    private String outfile;
    private List attrs;
...
}
public class ObjectAttributeModel {
    private String inalias;
    private String classname;
    private String outalias;
...
}
 
The test code:
        JavaObjectModel obj = new JavaObjectModel("lists", "com.xstream.acceptance.objects.SampleLists", "", "", new ArrayList());
        obj.getAttrs().add(new ObjectAttributeModel("software", "com.xstream.acceptance.objects.Software", "software"));
        obj.getAttrs().add(new ObjectAttributeModel("software", "com.xstream.acceptance.objects.Hardware", "software"));
        Configuration config = new Configuration(new ArrayList());
        config.getObjects().add(obj);
        XStream xs = new XStream(new DomDriver());
        xs.alias("config", Configuration.class);
        xs.alias("object", JavaObjectModel.class);
        xs.alias("attr", ObjectAttributeModel.class);
        String xml = xs.toXML(config);
        System.out.println(xml);
 
The XML outputing:
<XStream-Config>
  <objects>
    <object>
      <name>lists</name>
      <classname>com.achievo.acceptance.objects.SampleLists</classname>
      <infile></infile>
      <outfile></outfile>
      <attrs>
        <attr>
          <inalias>software</inalias>
          <classname>com.achievo.acceptance.objects.Software</classname>
          <outalias>software</outalias>
        </attr>
        <attr>
          <inalias>software</inalias>
          <classname>com.achievo.acceptance.objects.Hardware</classname>
          <outalias>software</outalias>
        </attr>
      </attrs>
    </object>
  </objects>
</XStream-Config>
 
What I wanted XML Outputing:
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile="">
      <attrs>
        <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
        <attr inalias="hardware" classname="com.achievo.acceptance.objects.Hardware" outalias="hardware"/>
      </attrs>
    </object>
  </objects>
</XStream-Config>
After I get this mail from Mauro. I try to add two converter for JavaObjectModel and ObjectAttributeModel to resolve this problem.  
public class JavaObjectModelConverter  implements Converter {
    public boolean canConvert(Class type) {
                 return type == JavaObjectModel.class;
    }
 
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        JavaObjectModel obj = (JavaObjectModel) source;
        writer.addAttribute("name", obj.getName());
        writer.addAttribute("classname", obj.getClassname());
        writer.addAttribute("infile", obj.getInfile());
        writer.addAttribute("outfile", obj.getOutfile());
    }
 
    public Object unmarshal(HierarchicalStreamReader reader,
                         UnmarshallingContext context, Class type)
    {
        String name = reader.getAttribute("name");
        String classname = reader.getAttribute("classname");
        String infile = reader.getAttribute("infile");
        String outfile = reader.getAttribute("outfile");
        return new JavaObjectModel(name, classname, infile, outfile);
    }
}
 
public class ObjectAttributeModelConverter  implements Converter {
    public boolean canConvert(Class type) {
                 return type == ObjectAttributeModel.class;
    }
 
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        ObjectAttributeModel obj = (ObjectAttributeModel) source;
        writer.addAttribute("inalias", obj.getInalias());
        writer.addAttribute("classname", obj.getClassname());
        writer.addAttribute("outalias", obj.getOutalias());
    }
 
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context, Class type)
    {
        String inalias = reader.getAttribute("inalias");
        String classname = reader.getAttribute("classname");
        String outalias = reader.getAttribute("outalias");
        return new ObjectAttributeModel(inalias, classname, outalias);
    }
}
 
Also I register these two converter to XStream.
xs.registerConverter(new JavaObjectModelConverter(), PRIORITY_NORMAL);
xs.registerConverter(new ObjectAttributeModelConverter(), PRIORITY_NORMAL);
And now I can only get the below outputing. It does not meet what I need.
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile=""/>
  </objects>
</XStream-Config>
 
I can get the below outputing (it is near by my purpose) if I just register one converter. But also it does not exactly equals my wanted. 
xs.registerConverter(new ObjectAttributeModelConverter(), PRIORITY_NORMAL);
 
<XStream-Config>
  <objects>
    <object>
      <name>lists</name>
      <classname>com.achievo.acceptance.objects.SampleLists</classname>
      <infile></infile>
      <outfile></outfile>
      <attrs>
        <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
        <attr inalias="software" classname="com.achievo.acceptance.objects.Hardware" outalias="software"/>
      </attrs>
    </object>
  </objects>
</XStream-Config>
 
What can I do to get the wanted outputing? Thanks a lot.
 
Regards.
Door.Xiao
 
----- Original Message -----
From: "Mauro Talevi" <[hidden email]>
Sent: Monday, November 14, 2005 1:41 AM
Subject: [xstream-user] Re: Deserialization with "value="

> Holger wrote:

>> How can I get data from an XML like this one below?
>>
>> <InHouse>
>>   <MessageTyp value="DELFOR">
>>   <DocNo value="53442">
>> </InHouse>
>>
>> By deserializing with the following statements an error occurs
>>
>>   XStream xs = new XStream(new DomDriver());
>>   xs.alias("InHouse",InHouse.class);
>>   String XMLStr = logMemo.getText();
>>   InHouse NewIH = (InHouse)xs.fromXML(XMLStr);
>>
>> testing by
>>
>>   logMemo.append(xs.toXML(NewAFPS));
>>
>> I thought to solve the problem by using the PropertyConverter
>>
>>   xs.registerConverter(new PropertiesConverter());
>>
>> The equal isn't different. What have I done wrong here?
>>
> The PropertiesConverter expects two attributes, name and value,
> and serialises Properties objects
>
> For your use case, you'd probably want to define your own converter
> and register it.
>
> Something like the following:
>
>     class MyObjectConverter implements Converter {
>
>         public boolean canConvert(Class type) {
>             return type == MyObjectConverter.class;
>         }
>
>         public void marshal(Object source, HierarchicalStreamWriter
> writer, MarshallingContext context) {
>             MyObject obj = (MyObject) source;
>             writer.addAttribute("value", obj.getValue());
>         }
>
>         public Object unmarshal(HierarchicalStreamReader reader,
> UnmarshallingContext context) {
>             String value = reader.getAttribute("value");
>             return new MyObject(value);
>         }
>     }
>
> Cheers
>
Reply | Threaded
Open this post in threaded view
|

[xstream-user] Re: Deserialization with "value="

Holger-3
In reply to this post by Mauro Talevi
If I interpreted your post the right way I have to implement a converter for
every property of a class. So if I have...

  <MessageTyp value="DELFOR">
  <DocNo value="53442">

...and now I say...

  public class AfpsInhouseConverter implements Converter {

    public boolean canConvert(Class type) {
        return type == AfpsInhouseConverter.class;
    }
   
    public void marshal(Object source, HierarchicalStreamWriter
                        writer, MarshallingContext context) {
        Datenaustauschkopf obj = (Datenaustauschkopf) source;
        writer.addAttribute("value", obj.getMessageTyp());
    }
   
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context) {
        String value = reader.getNodeName("value");
        return new MyObject(value);
    }
  }

... i've to repeat the line "writer.addAttribute("value", obj.getMadeBy());"
for every property of my class. But in every xml-Tga there is an "value=", so
how should I know the output is right or not, if I try this:

    public void marshal(Object source, HierarchicalStreamWriter
                        writer, MarshallingContext context) {
        Datenaustauschkopf obj = (Datenaustauschkopf) source;
        writer.addAttribute("value", obj.MessageTyp());
        writer.addAttribute("value", obj.DocNo());

    }

Or haver there to be ONE converter for EACH property of a class? Can you give
me an example how this can work?

Reply | Threaded
Open this post in threaded view
|

RE: [xstream-user] Re: Deserialization with "value="

Jörg Schaible
In reply to this post by Holger-3
Hi,

Door.Xiao wrote on Monday, November 14, 2005 9:05 AM:

> Good Idea. I met the other problem on the XML file outputing.
>
> There are two model classes:
> public class JavaObjectModel {
>     private String name;
>     private String classname;
>     private String infile;
>     private String outfile;
>     private List attrs;
> ...
> }

[snip]

> After I get this mail from Mauro. I try to add two converter for
> JavaObjectModel and ObjectAttributeModel to resolve this problem.
>     public class JavaObjectModelConverter  implements Converter {
>                  public boolean canConvert(Class type) { return type
>     == JavaObjectModel.class; }
>
>     public void marshal(Object source, HierarchicalStreamWriter
>                         writer, MarshallingContext context) {
>         JavaObjectModel obj = (JavaObjectModel) source;
>         writer.addAttribute("name", obj.getName());
>         writer.addAttribute("classname", obj.getClassname());
>         writer.addAttribute("infile", obj.getInfile());
>         writer.addAttribute("outfile", obj.getOutfile());
>     }
>
>     public Object unmarshal(HierarchicalStreamReader reader,
>                          UnmarshallingContext context, Class type)
>     {
>         String name = reader.getAttribute("name");
>         String classname = reader.getAttribute("classname");
>         String infile = reader.getAttribute("infile");
>         String outfile = reader.getAttribute("outfile");
>         return new JavaObjectModel(name, classname, infile, outfile);
>     }
> }

[snip]

Your Converter does not write/read anything into/from the stream for your "attrs" member. Look at the context.convertAnother method ...

- Jörg
Reply | Threaded
Open this post in threaded view
|

RE: [xstream-user] Re: Deserialization with "value="

Jörg Schaible
In reply to this post by Holger-3
Hi Holger,

Holger wrote on Monday, November 14, 2005 11:07 AM:

> If I interpreted your post the right way I have to implement
> a converter for
> every property of a class. So if I have...
>
>   <MessageTyp value="DELFOR">
>   <DocNo value="53442">
>
> ...and now I say...
>
>   public class AfpsInhouseConverter implements Converter {
>
>     public boolean canConvert(Class type) {
>         return type == AfpsInhouseConverter.class;
>     }

Note, that this is wrong. "canConvert" returns true if the type of the current object to convert can be handled by your converter. Since I don't suppose you will marshall an anstance of your Converter, this implementation gets useless here ;-)

>
>     public void marshal(Object source, HierarchicalStreamWriter
>                         writer, MarshallingContext context) {
>         Datenaustauschkopf obj = (Datenaustauschkopf) source;
>         writer.addAttribute("value", obj.getMessageTyp());     }
>
>     public Object unmarshal(HierarchicalStreamReader reader,
>                             UnmarshallingContext context) {
>         String value = reader.getNodeName("value");
>         return new MyObject(value);
>     }
>   }
>
> ... i've to repeat the line "writer.addAttribute("value",
> obj.getMadeBy());" for every property of my class. But in every
> xml-Tga there is an "value=", so how should I know the output is
> right or not, if I try this:
>
>     public void marshal(Object source, HierarchicalStreamWriter
>                         writer, MarshallingContext context) {
>         Datenaustauschkopf obj = (Datenaustauschkopf) source;
>         writer.addAttribute("value", obj.MessageTyp());
>         writer.addAttribute("value", obj.DocNo());
>
>     }
>
> Or haver there to be ONE converter for EACH property of a class? Can
> you give me an example how this can work?

You can implement a Converter for your InHouse object, that creates the appropriate nodes on its own or you have to provide a converter for each property (unless you write a more generic one - if possible).

Solution 1)

  public void marshal(...) {
        Datenaustauschkopf obj = (Datenaustauschkopf) source;
        writer.startNode("messageType");
        writer.addAttribute("value", obj.MessageTyp());
        writer.endNode();
        ...
  }


Solution 2)

  public boolean canConvert(Class type) {
        return type == MessageType.class || type == DocNo.class;
  }
  public void marshal(...) {
        writer.addAttribute("value", source.toString());
        ...
  }


- Jörg
Reply | Threaded
Open this post in threaded view
|

[xstream-user] Re: Deserialization with "value="

Holger-3
Ok, understand this. About reading the XML-file: If I register the following
converter, I've to fix an object (in my example the class "Datenaustauschkopf")

         public Object unmarshal(HierarchicalStreamReader reader,
                          UnmarshallingContext context) {
             String value = reader.getAttribute("value");
             return new MyObject(value);
         }
     }

When I read the file, is there a possibility to read all the "value"s from the
Tag "Datenaustauschkopf" like "StartNode" "EndNode" or something like this? If
not, do I have to create an converter for each property of
the "Datenaustauschkopf"-class? The problem is that I just specify the "value"-
String and not the Tag (e.g. MessageTyp, DocNo...) to read from. Can I read
the "Datenaustauschkopf"-Tag and all its properties in all?

Reply | Threaded
Open this post in threaded view
|

Re: [xstream-user] Re: Deserialization with "value="

Door.Xiao
In reply to this post by Jörg Schaible

Hi, Jörg
Thanks for your quick and professional answer. And I made a great progress on this problem reference what you point out at this mail.
 
I added the context.convertAnother(...) function just as the below code:
public class JavaObjectModelConverter  implements Converter {
    public boolean canConvert(Class type) {
                 return type == JavaObjectModel.class;
    }
 
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        JavaObjectModel obj = (JavaObjectModel) source;
        writer.addAttribute("name", obj.getName());
        writer.addAttribute("classname", obj.getClassname());
        writer.addAttribute("infile", obj.getInfile());
        writer.addAttribute("outfile", obj.getOutfile());
        writer.startNode("attrs");
        context.convertAnother(obj.getAttrs());
        writer.endNode();

    }
 
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context)
    {
        String name = reader.getAttribute("name");
        String classname = reader.getAttribute("classname");
        String infile = reader.getAttribute("infile");
        String outfile = reader.getAttribute("outfile");
        JavaObjectModel obj = new JavaObjectModel(name, classname, infile, outfile);
        Object attrs = null;
        attrs = context.convertAnother(reader.getAttribute("attrs"), List.class);
        if (attrs != null && attrs instanceof List)
        {
            obj.setAttrs((ArrayList)attrs);
        }
        return obj;
    }
}
 
Ok, Now, I can call toXML(...) to get what I really need:
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile="">
      <attrs>
        <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
        <attr inalias="hardware" classname="com.achievo.acceptance.objects.Hardware" outalias="hardware"/>
      </attrs>
    </object>
  </objects>
</XStream-Config>
 
But I can not get the object correctly using the abover XML file when I call fromXML(...) method. And now it throws exception as the below :
ConversionException: attrs : attrs
---- Debugging information ----
required-type       : java.util.ArrayList
cause-message       : attrs : attrs
class               :   Configuration
message             : attrs : attrs
line number         : 4
path                : /XStream-Config/objects/object/attrs
cause-exception     : CannotResolveClassException
-------------------------------
 
In fact, If I remark the two senteces "writer.startNode("attrs");" and "writer.endNode();" at the marshal(...) method, I can get the below outputing XML file:
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile="">
      <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
      <attr inalias="hardware" classname="com.achievo.acceptance.objects.Hardware" outalias="hardware"/>
    </object>
  </objects>
</XStream-Config>
 
And I also can get the surprised correct object using the abover XML file when I call fromXML(...) method at this time.
 
How can I do now? Accept this XML file or Modify the source code to write/read the standard XML file. Thanks anywhere.
 
Regards,
Door.Xiao
 
----- Original Message -----
From: "Jörg Schaible" <[hidden email]>
Sent: Monday, November 14, 2005 6:30 PM
Subject: RE: [xstream-user] Re: Deserialization with "value="

Hi,

Door.Xiao wrote on Monday, November 14, 2005 9:05 AM:

> Good Idea. I met the other problem on the XML file outputing.
>
> There are two model classes:
> public class JavaObjectModel {
>     private String name;
>     private String classname;
>     private String infile;
>     private String outfile;
>     private List attrs;
> ...
> }

[snip]

> After I get this mail from Mauro. I try to add two converter for
> JavaObjectModel and ObjectAttributeModel to resolve this problem.
>     public class JavaObjectModelConverter  implements Converter {
>                  public boolean canConvert(Class type) { return type
>     == JavaObjectModel.class; }
>
>     public void marshal(Object source, HierarchicalStreamWriter
>                         writer, MarshallingContext context) {
>         JavaObjectModel obj = (JavaObjectModel) source;
>         writer.addAttribute("name", obj.getName());
>         writer.addAttribute("classname", obj.getClassname());
>         writer.addAttribute("infile", obj.getInfile());
>         writer.addAttribute("outfile", obj.getOutfile());
>     }
>
>     public Object unmarshal(HierarchicalStreamReader reader,
>                          UnmarshallingContext context, Class type)
>     {
>         String name = reader.getAttribute("name");
>         String classname = reader.getAttribute("classname");
>         String infile = reader.getAttribute("infile");
>         String outfile = reader.getAttribute("outfile");
>         return new JavaObjectModel(name, classname, infile, outfile);
>     }
> }

[snip]

Your Converter does not write/read anything into/from the stream for your "attrs" member. Look at the context.convertAnother method ...

- Jörg
Reply | Threaded
Open this post in threaded view
|

Re: [xstream-user] Re: Deserialization with "value="

Door.Xiao

Ok, I get it now. I just need to move the pointer of elementStack to skip the "attrs" noede. Thanks anywhere.
 
public class JavaObjectModelConverter  implements Converter {
    public boolean canConvert(Class type) {
                 return type == JavaObjectModel.class;
    }
 
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        JavaObjectModel obj = (JavaObjectModel) source;
        writer.addAttribute("name", obj.getName());
        writer.addAttribute("classname", obj.getClassname());
        writer.addAttribute("infile", obj.getInfile());
        writer.addAttribute("outfile", obj.getOutfile());
        writer.startNode("attrs");
        context.convertAnother(obj.getAttrs());
        writer.endNode();

    }
 
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context, Class type)
    {
        String name = reader.getAttribute("name");
        String classname = reader.getAttribute("classname");
        String infile = reader.getAttribute("infile");
        String outfile = reader.getAttribute("outfile");
        JavaObjectModel obj = new JavaObjectModel(name, classname, infile, outfile);
        reader.moveDown();
        Object attrs = null;
        attrs = context.convertAnother(reader.getAttribute("attrs"), ArrayList.class);
        if (attrs != null && attrs instanceof List)
        {
            obj.setAttrs((ArrayList)attrs);
        }
        reader.moveUp();

        return obj;
    }
}
 
Now I can read/write the standard XML file like the below:
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile="">
      <attrs>
        <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
        <attr inalias="hardware" classname="com.achievo.acceptance.objects.Hardware" outalias="hardware"/>
      </attrs>
    </object>
  </objects>
</XStream-Config>
  
 
Regards,
Door.Xiao
----- Original Message -----
Sent: Monday, November 14, 2005 7:46 PM
Subject: Re: [xstream-user] Re: Deserialization with "value="

Hi, Jörg
Thanks for your quick and professional answer. And I made a great progress on this problem reference what you point out at this mail.
 
I added the context.convertAnother(...) function just as the below code:
public class JavaObjectModelConverter  implements Converter {
    public boolean canConvert(Class type) {
                 return type == JavaObjectModel.class;
    }
 
    public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
        JavaObjectModel obj = (JavaObjectModel) source;
        writer.addAttribute("name", obj.getName());
        writer.addAttribute("classname", obj.getClassname());
        writer.addAttribute("infile", obj.getInfile());
        writer.addAttribute("outfile", obj.getOutfile());
        writer.startNode("attrs");
        context.convertAnother(obj.getAttrs());
        writer.endNode();

    }
 
    public Object unmarshal(HierarchicalStreamReader reader,
                            UnmarshallingContext context)
    {
        String name = reader.getAttribute("name");
        String classname = reader.getAttribute("classname");
        String infile = reader.getAttribute("infile");
        String outfile = reader.getAttribute("outfile");
        JavaObjectModel obj = new JavaObjectModel(name, classname, infile, outfile);
        Object attrs = null;
        attrs = context.convertAnother(reader.getAttribute("attrs"), List.class);
        if (attrs != null && attrs instanceof List)
        {
            obj.setAttrs((ArrayList)attrs);
        }
        return obj;
    }
}
 
Ok, Now, I can call toXML(...) to get what I really need:
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile="">
      <attrs>
        <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
        <attr inalias="hardware" classname="com.achievo.acceptance.objects.Hardware" outalias="hardware"/>
      </attrs>
    </object>
  </objects>
</XStream-Config>
 
But I can not get the object correctly using the abover XML file when I call fromXML(...) method. And now it throws exception as the below :
ConversionException: attrs : attrs
---- Debugging information ----
required-type       : java.util.ArrayList
cause-message       : attrs : attrs
class               :   Configuration
message             : attrs : attrs
line number         : 4
path                : /XStream-Config/objects/object/attrs
cause-exception     : CannotResolveClassException
-------------------------------
 
In fact, If I remark the two senteces "writer.startNode("attrs");" and "writer.endNode();" at the marshal(...) method, I can get the below outputing XML file:
<XStream-Config>
  <objects>
    <object name="lists" classname="com.achievo.acceptance.objects.SampleLists" infile="" outfile="">
      <attr inalias="software" classname="com.achievo.acceptance.objects.Software" outalias="software"/>
      <attr inalias="hardware" classname="com.achievo.acceptance.objects.Hardware" outalias="hardware"/>
    </object>
  </objects>
</XStream-Config>
 
And I also can get the surprised correct object using the abover XML file when I call fromXML(...) method at this time.
 
How can I do now? Accept this XML file or Modify the source code to write/read the standard XML file. Thanks anywhere.
 
Regards,
Door.Xiao
 
----- Original Message -----
From: "Jörg Schaible" <[hidden email]>
Sent: Monday, November 14, 2005 6:30 PM
Subject: RE: [xstream-user] Re: Deserialization with "value="

Hi,

Door.Xiao wrote on Monday, November 14, 2005 9:05 AM:

> Good Idea. I met the other problem on the XML file outputing.
>
> There are two model classes:
> public class JavaObjectModel {
>     private String name;
>     private String classname;
>     private String infile;
>     private String outfile;
>     private List attrs;
> ...
> }

[snip]

> After I get this mail from Mauro. I try to add two converter for
> JavaObjectModel and ObjectAttributeModel to resolve this problem.
>     public class JavaObjectModelConverter  implements Converter {
>                  public boolean canConvert(Class type) { return type
>     == JavaObjectModel.class; }
>
>     public void marshal(Object source, HierarchicalStreamWriter
>                         writer, MarshallingContext context) {
>         JavaObjectModel obj = (JavaObjectModel) source;
>         writer.addAttribute("name", obj.getName());
>         writer.addAttribute("classname", obj.getClassname());
>         writer.addAttribute("infile", obj.getInfile());
>         writer.addAttribute("outfile", obj.getOutfile());
>     }
>
>     public Object unmarshal(HierarchicalStreamReader reader,
>                          UnmarshallingContext context, Class type)
>     {
>         String name = reader.getAttribute("name");
>         String classname = reader.getAttribute("classname");
>         String infile = reader.getAttribute("infile");
>         String outfile = reader.getAttribute("outfile");
>         return new JavaObjectModel(name, classname, infile, outfile);
>     }
> }

[snip]

Your Converter does not write/read anything into/from the stream for your "attrs" member. Look at the context.convertAnother method ...

- Jörg
Reply | Threaded
Open this post in threaded view
|

RE: [xstream-user] Re: Deserialization with "value="

Jörg Schaible
In reply to this post by Holger-3
Door.Xiao wrote on Monday, November 14, 2005 1:13 PM:

> Ok, I get it now. I just need to move the pointer of elementStack to
> skip the "attrs" noede. Thanks anywhere.

Fine. I was about to suggest this ... :)

- Jörg
Reply | Threaded
Open this post in threaded view
|

RE: [xstream-user] Re: Deserialization with "value="

Jörg Schaible
In reply to this post by Holger-3
Hi Holger,

Holger wrote on Monday, November 14, 2005 12:45 PM:

> Ok, understand this. About reading the XML-file: If I
> register the following
> converter, I've to fix an object (in my example the class
> "Datenaustauschkopf")
>
>          public Object unmarshal(HierarchicalStreamReader reader,
>                           UnmarshallingContext context) {
>              String value = reader.getAttribute("value");
>              return new MyObject(value);
>          }
>      }
>
> When I read the file, is there a possibility to read all the "value"s
> from the Tag "Datenaustauschkopf" like "StartNode" "EndNode" or
> something like this?

There is a reader method ... getNodeName? Don't know by heart, but yes, it is definately possible to get the name of the current tag.

> If
> not, do I have to create an converter for each property of
> the "Datenaustauschkopf"-class? The problem is that I just
> specify the "value"- String and not the Tag (e.g. MessageTyp,
> DocNo...) to read from. Can I read
> the "Datenaustauschkopf"-Tag and all its properties in all?

may be you should have a look at the PropertiesConverter implemenattion, that does something very similar.

Regards,
Jörg