xml parsing - Java Simple XML Converter write element to OutputNode -


simple xml allows make own converter.

this write method used serialize object xml. serialization should performed in such way of objects values represented element or attribute of provided node. ensures can deserialized @ later time.

@convert(myclass.converter.class) public class myclass {   public myclass() {}    public static class converter implements converter<myclass> {     public myclass read(inputnode node) {       return new myclass();     }      public void write(outputnode node, myclass value) {      }   } } 

the documentation describes representing element in outputnode, how add element outputnode? documentation outputnode doesn't appear have methods add element or node it.

you can either add elements hand or use serializer add -- serializer can write/read to/from nodes.

here's example:

@root(name = "myclass") // @root required @convert(myclass.myclassconverter.class) public class myclass {     private int value;     private string name;      /* ctor, getter, setter etc. */       public static class myclassconverter implements converter<myclass>     {         @override         public myclass read(inputnode node) throws exception         {             myclass mc = new myclass();              /* read (int) value of 'somevalue' node */             int value = integer.parseint(node.getnext("somevalue").getvalue());             mc.setvalue(value);              /* same string */             string name = node.getnext("somestring_" + value).getvalue();             mc.setname(name);              /* data not used in myclass, useable anyway */             if( node.getnext("from") == null )             {                 throw new illegalargumentexception("node 'from' missing!");             }              return mc;         }           @override         public void write(outputnode node, myclass value) throws exception         {             /* add attribute root node */             node.setattribute("example", "true");              outputnode valuenode = node.getchild("somevalue");      // create child node ...             valuenode.setattribute("stack", "overflow");            // ... attribute             valuenode.setvalue(string.valueof(value.getvalue()));   // ... , value              /*              * converter allow dynamic creation -- here node names              * created 2 values of myclass              */             node.getchild("somestring_" + value.getvalue())     // create child node ...                     .setvalue(value.getname());                 // ... value              /* create node scratch */             outputnode fromnode = node.getchild("from");             fromnode.setvalue("scratch");             fromnode.setcomment("this node created converter");         }      }  } 

the xml write / read:

<myclass example="true">    <somevalue stack="overflow">27</somevalue>    <somestring_27>abc</somestring_27>    <!-- node created converter -->    <from>scratch</from> </myclass> 

important: have use annotationstrategy, otherwise @convert wont work.

serializer ser = new persister(new annotationstrategy()) ser.write(...); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -