java - How to read list elements with attribute via XStream -


i'm using xstream read below example xml file.

<list>     <file>/setup/x86-linux2/bin/zip.txt</file>     <file type="dir">/src/bin/</file>     <name>test xml</name> </list> 

below code reading above xml,

public class listwithconverter {      public static class fileconvertor implements converter {         public boolean canconvert(final class clazz) {             return clazz.equals(myfile.class);         }          public void marshal(object source, hierarchicalstreamwriter writer,                 marshallingcontext context) {             throw new unsupportedoperationexception("not supported write file element yet."); //$non-nls-1$         }          public object unmarshal(hierarchicalstreamreader reader,                 unmarshallingcontext context) {             myfile file = new myfile();             (iterator<string> iter = reader.getattributenames(); iter.hasnext(); ){                 string name = iter.next();                 if (name.equals("type")) //$non-nls-1$                     file.type = reader.getattribute(name);             }             file.path = reader.getvalue();             return file;         }     }      @xstreamalias("list")     public class mylist {         @xstreamalias("name")         string name;         @xstreamimplicit(itemfieldname="file")  @xstreamconverter(fileconvertor.class)         list<myfile> files;     }      public static class myfile {         string type;         string path;     }      public static void main(string[] args) throws malformedurlexception, ioexception {         xstream xstream = new xstream();         xstream.setclassloader(mylist.class.getclassloader());         xstream.processannotations(mylist.class);         inputstream stream = new file("test.xml").tourl().openstream();         mylist list = (mylist)xstream.fromxml(stream);         system.out.println(list.name);         (myfile f : list.files) {             system.out.println(f.path);         }     } } 

the output of program is,

test xml null null 

looks xstream not support using annotation '@xstreamimplicit' , '@xstreamconverter' @ same time.

my question how should read example xml via xstream?

i found solution after migrating xstream 1.4.x,

@xstreamalias("list") public class mylist {     @xstreamalias("name")     string name;     @xstreamimplicit(itemfieldname="file")     list<myfile> files; }  @xstreamalias("file") @xstreamconverter(value=toattributedvalueconverter.class, strings={"path"}) public static class myfile {     @xstreamalias("type")     @xstreamasattribute     string type;      string path; } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -