Simple xml

需要解析的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;
	//   }
}


自定義List

import 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>


自定義 Entry

import 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();
	}
	
	}
	
}

發佈了49 篇原創文章 · 獲贊 8 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章