基于Java StAX光标的API读取XML示例
时间:2020-02-23 14:34:31 来源:igfitidea点击:
Java StAX API提供了两个XML处理API –基于游标和基于迭代器。
前面我们看到了基于迭代器的API的示例,该API可以读取XML文件和写入XML文件。
其中我们将学习如何使用基于游标的API来读取XML文件。
当我们使用StAX XML Parser时,我们需要创建XMLInputFactory
来读取XML文件。
然后我们可以通过创建XMLStreamReader对象来读取文件来选择基于游标的API。
XMLStreamReader next()方法用于获取下一个解析事件,并根据事件类型返回int值。
常见事件类型是"开始文档","开始元素","字符","结束元素"和"结束文档"。
XMLStreamConstants包含int常量,可用于根据其类型处理事件。
对于本教程,我们在下面的xml文件中列出了Employee元素。
<?xml version="1.0" encoding="UTF-8"?> <Employees> <Employee id="1"> <age>29</age> <name>hyman</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>35</age> <name>Lisa</name> <gender>Female</gender> <role>CEO</role> </Employee> <Employee id="3"> <age>40</age> <name>Tom</name> <gender>Male</gender> <role>Manager</role> </Employee> <Employee id="4"> <age>25</age> <name>Meghna</name> <gender>Female</gender> <role>Manager</role> </Employee> </Employees>
我们有Employee类来表示Employee元素。
package com.theitroad.xml; public class Employee { private int id; private String name; private String gender; private int age; private String role; 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 String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "Employee:: ID="+this.id+" Name=" + this.name + " Age=" + this.age + " Gender=" + this.gender + " Role=" + this.role; } }
这是我们使用StAX XMLStreamReader读取xml文件到Employee对象列表的类。
package com.theitroad.xml.stax; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import com.theitroad.xml.Employee; public class StaxXMLStreamReader { private static boolean bName; private static boolean bAge; private static boolean bGender; private static boolean bRole; public static void main(String[] args) { String fileName = "/Users/hyman/employees.xml"; List<Employee> empList = parseXML(fileName); for(Employee emp : empList){ System.out.println(emp.toString()); } } private static List<Employee> parseXML(String fileName) { List<Employee> empList = new ArrayList<>(); Employee emp = null; XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); try { XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new FileInputStream(fileName)); int event = xmlStreamReader.getEventType(); while(true){ switch(event) { case XMLStreamConstants.START_ELEMENT: if(xmlStreamReader.getLocalName().equals("Employee")){ emp = new Employee(); emp.setId(Integer.parseInt(xmlStreamReader.getAttributeValue(0))); }else if(xmlStreamReader.getLocalName().equals("name")){ bName=true; }else if(xmlStreamReader.getLocalName().equals("age")){ bAge=true; }else if(xmlStreamReader.getLocalName().equals("role")){ bRole=true; }else if(xmlStreamReader.getLocalName().equals("gender")){ bGender=true; } break; case XMLStreamConstants.CHARACTERS: if(bName){ emp.setName(xmlStreamReader.getText()); bName=false; }else if(bAge){ emp.setAge(Integer.parseInt(xmlStreamReader.getText())); bAge=false; }else if(bGender){ emp.setGender(xmlStreamReader.getText()); bGender=false; }else if(bRole){ emp.setRole(xmlStreamReader.getText()); bRole=false; } break; case XMLStreamConstants.END_ELEMENT: if(xmlStreamReader.getLocalName().equals("Employee")){ empList.add(emp); } break; } if (!xmlStreamReader.hasNext()) break; event = xmlStreamReader.next(); } } catch (FileNotFoundException | XMLStreamException e) { e.printStackTrace(); } return empList; } }
当我们执行上面的程序时,我们在控制台中得到以下输出。
Employee:: ID=1 Name=hyman Age=29 Gender=Male Role=Java Developer Employee:: ID=2 Name=Lisa Age=35 Gender=Female Role=CEO Employee:: ID=3 Name=Tom Age=40 Gender=Male Role=Manager Employee:: ID=4 Name=Meghna Age=25 Gender=Female Role=Manager