XML 之解析sax(2) 解析到一个对象中


http://blog.csdn.net/qq_33599978/article/details/68938567

public class Inproe {
	
	public static void main(String[] args) {
		SAXParserFactory sFactory = SAXParserFactory.newInstance();
		try {
			SAXParser parser =  sFactory.newSAXParser();
			File file = new File("src/com/xm/inproe.xml");
			MyHandy mh = new MyHandy();
			parser.parse(file, mh);
			List<Employee> list = mh.getList();
		//	System.out.println(list);
			for (Employee employee : list) {
				System.out.println(employee);
			}
		} catch (ParserConfigurationException | SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

class MyHandy extends DefaultHandler{
	private List<Employee> list;
	private String tagName;  //记录标签的值 如sy  <sy> </sy>
	private Employee employee;
	public List<Employee> getList() {
		return list;
	}
	public void setList(List<Employee> list) {
		this.list = list;
	}
	
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		
		if ("employee".equals(qName)) {
			list.add(employee);
			//System.out.println(list);
		} else {
			//记录标签的值 如sy  <sy> </sy>
			//防止每个结束标签的时候将换行  赋值给tagName
			tagName=null;
		}
	}
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		
		if ("employee".equals(qName)) {
			employee= new Employee();
			employee.setId(Integer.parseInt(attributes.getValue("id")));
			employee.setDepName(attributes.getValue("depName"));
		}else {
			tagName=qName;
			
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
	
		String str = new String(ch, start, length);
		if ("name".equals(tagName)) {
			employee.setName(str);
		}else if("age".equals(tagName)){
			employee.setAge(Integer.parseInt(str));
		}else if ("gender".equals(tagName)) {
			employee.setGender(str);
		}else if ("salary".equals(tagName)) {
			employee.setSalary(Integer.parseInt(str)); 
		}
		
	}
	@Override
	public void startDocument() throws SAXException {
		
		list = new ArrayList<Employee>();
	}
	
}


public class Employee {

	private int id;
	private String name;
	private String gender;
	private int age;
	private int salary;
	private String depName;
	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 String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String getDepName() {
		return depName;
	}
	public void setDepName(String depName) {
		this.depName = depName;
	}
	public Employee(int id, String name, String gender, int age, int salary, String depName) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.age = age;
		this.salary = salary;
		this.depName = depName;
	}
	public Employee() {
	}
	
	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", salary=" + salary
				+ ", depName=" + depName + "]";
	}
	

}


<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <employee id="1" depName="教学部">
       Text XMl sax
        <name>tom</name>
        <age>18</age>
        <gender>male</gender>
        <salary>200</salary>
        <test>test</test>
    </employee>
    
     <employee id="2" depName="教学部">
        <name>java</name>
        <age>19</age>
        <gender>famale</gender>
        <salary>2050</salary>
    </employee>
    
     <employee id="3" depName="市场部">
        <name>store</name>
        <age>20</age>
        <gender>famale</gender>
        <salary>4000</salary>
    </employee>
    
     
    <employee id="4" depName="市场部">
        <name>buy</name>
        <age>21</age>
        <gender>male</gender>
        <salary>45500</salary>
    </employee>
    
</employees>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值