使用Apache的Digester来解析XML文档

当前用的Digester1.8 需要三个jar commons-digester-1.8.jar,commons-beanutils.jar,commons-logging-1.0.4.jar http://download.csdn.net/detail/wangjinwei6912/7610565
下面上代码
package com.wckj.model;

public class Area {
	private int id;
	private String name;
	private String areaType;
	private int parentId;
	private int ordering;
	private String zip;

	private String phoneArea;

	public int getOrdering() {
		return ordering;
	}

	public void setOrdering(int ordering) {
		this.ordering = ordering;
	}

	public String getAreaType() {
		return areaType;
	}

	public void setAreaType(String areaType) {
		this.areaType = areaType;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getParentId() {
		return parentId;
	}

	public void setParentId(int parentId) {
		this.parentId = parentId;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public String getPhoneArea() {
		return phoneArea;
	}

	public void setPhoneArea(String phoneArea) {
		this.phoneArea = phoneArea;
	}

}
package com.wckj.entity;

import java.util.ArrayList;
import java.util.List;

import com.wckj.model.Area;

public class ViewCache {
	private List areaList = new ArrayList();

	public List getAreaList() {
		return areaList;
	}

	public void setAreaList(List areaList) {
		this.areaList = areaList;
	}

	// 供Digester调用的方法
	public void addArea(Area area) {
		this.areaList.add(area);
	}

}

package com.wckj.entity;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import com.wckj.model.Area;

public class AreaDigester {
	public ViewCache digester(String xml) throws Exception {
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.addObjectCreate("viewcache/areas", ViewCache.class);
		// 指明匹配模式和要创建的类
		digester.addObjectCreate("viewcache/areas/area", Area.class);
		// 设置对象属性,与xml文件对应,不设置则是默认
		digester.addBeanPropertySetter("viewcache/areas/area/id", "id");
		digester.addBeanPropertySetter("viewcache/areas/area/parentId",
				"parentId");
		digester.addBeanPropertySetter("viewcache/areas/area/name", "name");
		digester.addBeanPropertySetter("viewcache/areas/area/areaType",
				"areaType");
		digester.addBeanPropertySetter("viewcache/areas/area/ordering",
				"ordering");
		digester.addBeanPropertySetter("viewcache/areas/area/zip", "zip");
		digester.addBeanPropertySetter("viewcache/areas/area/phoneArea",
				"phoneArea");
		// 当移动到下一个标签中时的动作
		digester.addSetNext("viewcache/areas/area", "addArea");

		ViewCache vc = null;
		try {
//			vc = (ViewCache) digester.parse(new File("e:\\viewcache.xml"));
			vc = (ViewCache) digester.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
		} catch (IOException e) {
			throw new Exception(e);
		} catch (SAXException e) {
			throw new Exception(e);
		}
		return vc;
	}
	
	public static void main(String[] args) throws Exception {
		AreaDigester d = new AreaDigester();
//		ViewCache v = d.digester();
		String xml ="".concat("	10981001province北京1867");
		ViewCache v = d.digester(xml);
		System.out.println(v);
	}
}


 
  
 
  

你可能感兴趣的:(java开发)