需要解析的xml
<?xml version="1.0" encoding="UTF-8"?> <propertyList > <list> <count>15</count> <entry key="one"> <value>first value</value> </entry> <entry key="two"> <value>first value</value> </entry> <entry key="three"> <value>first value</value> </entry> <entry key="four"> <value>first value</value> </entry> </list> </propertyList> 解析类 PropertyList
import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root public class PropertyList { public PropertyList() { super(); } @Element private List list ; public List getList() { return list; } public void setList(List list) { this.list = list; } // public String getName() { // return name; // } // // public List getProperties() { // return list; // } }
自定义Listimport org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.ElementList; public class List { public List() { super(); // TODO Auto-generated constructor stub } @Element private String count; @ElementList(inline=true) private java.util.List<Entry> list; @Attribute(required=false) private String name; public String getCount() { return count; } public void setCount(String count) { this.count = count; } public java.util.List<Entry> getList() { return list; } public void setList(java.util.List<Entry> list) { this.list = list; } public String getName() { return name; } public void setName(String name) { this.name = name; } } <?xml version="1.0" encoding="UTF-8"?> <propertyList > <list> <count>15</count> <entry key="one"> <value>first value</value> </entry> <entry key="two"> <value>first value</value> </entry> <entry key="three"> <value>first value</value> </entry> <entry key="four"> <value>first value</value> </entry> </list> </propertyList>
自定义 Entryimport org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root public class Entry { public Entry() { super(); // TODO Auto-generated constructor stub } @Attribute private String key; @Element private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } // public String getName() { // return name; // } // // public String getValue() { // return value; // } }
解析 方法
import java.io.File; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; public class ProcessList { public static void main(String[] args) { Serializer serializer = new Persister(); File source = new File("list.xml"); try { PropertyList example = serializer.read(PropertyList.class, source); System.out.println(example.getList().getCount()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Simple xml
最新推荐文章于 2021-05-26 07:54:54 发布