XML 之解析sax(2) 解析到一个对象中
2017-03-31 23:17
393 查看
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>
相关文章推荐
- 【慕课笔记】3-4 应用SAX方式解析XML—使用SAX解析将XML的内容和结构存入JAVA对象
- SAX解析xml文档经常讲xml变成对象
- javascript把特定XML响应解析成一个对象
- xml之SAX解析以及数据结构存入java对象
- XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象
- XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象
- 一个android中使用sax解析xml的例子
- Layout inflation在Android环境中是一个专业术语,用于表示某个XML资源布局文件被解析并转化成为一个层级的View对象的过程。
- 在android开发中,经常用到去解析xml文件,常见的解析xml的方式有一下三种:SAX、Pull、Dom解析方式。最近做了一个android版的CSDN阅读器,用到了其中的两种(sax,pull)
- SAX 解析XML文件:将XML转换成Java对象
- 一个SAX解析xml的简单程序。
- Java中Xml解析详解 DOM、SAX、JDOM、DOM4J
- SAX解析多层嵌套XML
- xml基础--解析(SAX)
- XML数据解析的两种方法:SAX和DOM
- 简说XML的解析方式(DOM,SAX,StAX)
- Android--入门---16---(Android中xml 的生成 & 解析---DOM . SAX . PULL)
- 解析一个XML文件-Yahoo天气(xml文件有属性值)
- XML两种解析方式:SAX和DOM性能对比
- 用SAXReader解析xml文档