In some cases, the XML file is like this
<body>
<entryList>
<field name= "Forumname" >general discussion</field>
< Field name= "CreateDate" >2013-09-11 12:37:02</field>
</entryList>
</body>
Parsing with XML parsing tools is good for this XML, but cannot be tied directly to XML binding tools. Minor modifications are required.
If you are using XStream, you will add a converter to implement the converter interface. First post the interface:
Import Com.thoughtworks.xstream.converters.Converter;
Import Com.thoughtworks.xstream.converters.MarshallingContext;
Import Com.thoughtworks.xstream.converters.UnmarshallingContext;
Import Com.thoughtworks.xstream.io.HierarchicalStreamReader;
Import Com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class Fieldconverter implements Converter {@Override public boolean Canconvert (@SuppressWarnings ("Rawtypes") Cl
Ass type) {if (type = = Field.class) {return true;
return false; @Override public void Marshal (Object source, hierarchicalstreamwriter writer, marshallingcontext context) {Fiel
d field = (field) source;
Writer.startnode ("field");
Writer.addattribute ("Name", Field.getname ());
Writer.setvalue (Field.getvalue ());
Writer.endnode (); @Override public Object unmarshal (Hierarchicalstreamreader Reader, Unmarshallingcontext context) {Field field =
New Field ();
String name = Reader.getattribute ("name");
Field.setname (name); String value = reader.getvalue ();
Field.setvalue (value);
return field; }
}
Here are two Pojo package classes:
Import java.util.List;
public class Body {
list<field> entrylist;
Public Body () {
super ();
}
Public list<field> getentrylist () {return
entrylist;
}
public void Setentrylist (list<field> entrylist) {
this.entrylist = entrylist;
}
}
The following filed class is used to encapsulate the inside field field, name corresponds to the name attribute value, and value corresponds to the filed. Standard practice should have a <value> tag.
public class Field {
String name;
String value;
Public Field () {
super ();
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String GetValue () {return
value;
}
public void SetValue (String value) {
this.value = value;
}
}
The following code is the main method, which is used to test:
Import java.util.ArrayList;
Import java.util.List;
Import Com.thoughtworks.xstream.XStream;
public class Testxml {public
static void Main (string[] aa) {
list<field> entrylist = new Arraylist<field > ();
Field field = new Field ();
Field.setname ("Forumname");
Field.setvalue ("General Discussion");
Entrylist.add (field);
Field field2 = new Field ();
Field2.setname ("CreateDate");
Field2.setvalue ("2013-09-11 12:37:02");
Entrylist.add (field2);
BODY = new Body ();
Body.setentrylist (entrylist);
XStream XStream = new XStream ();
Xstream.registerconverter (New Fieldconverter ());
Xstream.alias ("field", Field.class);
Xstream.alias ("Body", body.class);
String XML = xstream.toxml (body);
SYSTEM.ERR.PRINTLN (XML);
Body Body2 = (body) xstream.fromxml (XML);
System.out.println (body2);
}
If you do not use Fieldconverter, the output XML value value is labeled and does not conform to the rule by specifying aliases and attributes. When used, the properties and values with field are output, and value is directly the value of the field and will not appear in the <value> label.