爪哇国新游记之十三----XML文件读写

/**
 * XML读写示例
 * @author hx
 *
 */
public class XmlReaderWriter{
    /**
     * 读取一个XML文件,返回一个雇员链表
     * @param fileName
     * @return
     */
    public List<Employee> readXml(String fileName){
        List<Employee> employees=new ArrayList<Employee>();
        
        SAXReader reader = new SAXReader();
        try {
            Document  document = reader.read(new File(fileName));
            Element root = document.getRootElement();
            
            List nodes = root.elements("employee");

            for (Iterator it = nodes.iterator(); it.hasNext();) {
                Employee employee=new Employee();
                
                // 根节点下的雇员子节点
                Element employeeElm = (Element) it.next();
                
                // 获取雇员子节点的id属性
                Attribute attribute=employeeElm.attribute("id");
                employee.setId(Long.parseLong(attribute.getText()));// 将文本形式的long字符串还原成基本类型
                
                // 获得雇员子节点的名称子节点
                Element nameElm=employeeElm.element("name");
                employee.setName(nameElm.getText());// 获取名称子节点的文字
                
                // 获取雇员子节点的性别子节点的文字
                employee.setMale(Boolean.parseBoolean(employeeElm.element("ismale").getText()));
                
                // 将雇员信息放入链表
                employees.add(employee);
            }
            
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        return employees;
    }
    
    /**
     * 将一个雇员链表写入XML文档
     * @param employees
     * @param fileName
     */
    public void writerToFile(List<Employee> employees,String fileName){
        // 创建文档对象
        Document document = DocumentHelper.createDocument();
        // 创建根节点
        Element root = document.addElement("employees");
        
        for(Employee emp:employees){
            // 创建employee节点
            Element empElm = root.addElement("employee");
            
            // 创建employee节点的id属性
            empElm.addAttribute("id", String.valueOf(emp.getId()));
            
            // 创建employee节点的子节点name
            Element nameElm = empElm.addElement("name");
            nameElm.setText(emp.getName());// 设置子节点的文字
            
            // 创建employee节点的子节点ismale            
            Element ismaleElm = empElm.addElement("ismale");
            ismaleElm.setText(String.valueOf(emp.isMale()));// 设置子节点的文字
        }
        
        // 将document文档对象写入文件
        XMLWriter writer;
        try {
            writer = new XMLWriter(new FileWriter(fileName));
            writer.write(document);
            writer.close();
                
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
    
    public static void main(String[] args){
    List<Employee> employees=new ArrayList<Employee>();
        /*employees.add(new Employee(1,"Andy",true));
        employees.add(new Employee(2,"Bill",false));
        employees.add(new Employee(3,"Cindy",true));
        employees.add(new Employee(4,"Doutglas",false));
        
        XmlReaderWriter xmlReaderWriter=new XmlReaderWriter();
        xmlReaderWriter.writerToFile(employees, "Employees20140724.xml");*/
        
        XmlReaderWriter xmlReaderWriter=new XmlReaderWriter();
        List<Employee> employee=xmlReaderWriter.readXml("Employees20140724.xml");
        employee=null;
        
    }
}
public class Employee{
    private long id;
    private String name;
    private boolean isMale;
    
    public Employee(){
        
    }
    
    public Employee(long id,String name,boolean isMale){
        this.id=id;
        this.name=name;
        this.isMale=isMale;
    }
    
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public boolean isMale() {
        return isMale;
    }
    public void setMale(boolean isMale) {
        this.isMale = isMale;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

这个的主要目的是让学生掌握如何导入第三方包。

你可能感兴趣的:(文件读写)