• 爪哇国新游记之十三----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;
        }
    }

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

  • 相关阅读:
    suging闲谈netty 的异步非阻塞IO线程与业务线程分离
    实战 | 关于随手一点就发现XSS漏洞这件事
    Java安全第一篇 | 反射看这一篇就够了
    工具推荐 | Xshell全版本解密工具(包括Xshell7)——SharpXDecrypt
    InfluxDB 2.x Open Source Time Series Database
    Grafana v8.3.3 & jmeterinfluxdb2backend
    【聊聊认知】你的问题是什么
    第5章着色基础5.4锯齿和抗锯齿
    第3章图形处理单元3.8像素着色器
    面向对象编程(C++篇2)——构造
  • 原文地址:https://www.cnblogs.com/heyang78/p/3867513.html
Copyright © 2020-2023  润新知