我爱编程

Java创建新的XML和读取,写入

2017-07-13  本文已影响0人  jose_dl

xml作为一种最基本的数据保存方式,可以用用。。。虽然有毒

        user对象保存一些属性,以便读取
        UUID randomUUID = UUID.randomUUID();//uid
        user.setUid(randomUUID.toString());
 // 想把xml文件写入到WEB-INF/user.xml。。。但是WEB-INF这个文件夹有坑
        String contextPath = getServletContext().getContextPath();
        String realPath = getServletContext().getRealPath("WEB-INF/user.xml");
        File xmlFile = new File(realPath);
       
        if (!xmlFile.exists()) {
             Element root = DocumentHelper.createElement("users");  
             Document document = DocumentHelper.createDocument(root); 
           //创建文件  ,格式化是的xml文件更有层次
             OutputFormat format = OutputFormat.createPrettyPrint();  
            FileOutputStream file = new FileOutputStream(xmlFile);  
            XMLWriter xml = new XMLWriter(file,format);  
              xml.write(document);
            xml.close(); 
        }
//解析xml,刚才创建,现在读取,从根节点开始,给添加user元素。
//然后疯狂添加属性,也可以换成添加子元素,
        SAXReader reader = new SAXReader();
        try {
            Document d = (Document) reader.read(xmlFile);
            Element root = (Element) d.selectSingleNode("users");
            Element userElement = root.addElement("user");
            
            Element aUsername = userElement.addAttribute("username", user.getUsername());
            Element aPassword = userElement.addAttribute("password", user.getPassword());
            Element aGender = userElement.addAttribute("gender", user.getGender());
            Element aAge = userElement.addAttribute("age", user.getAge());
            Element aEmail = userElement.addAttribute("email", user.getEmail());
            Element aPic = userElement.addAttribute("pic", user.getPic());
            Element aUid = userElement.addAttribute("uid", user.getUid());
            //这一句可以让xml文件看上去格式正常
            OutputFormat format = OutputFormat.createPrettyPrint();  

            FileOutputStream file = new FileOutputStream(xmlFile);  
            XMLWriter xml = new XMLWriter(file,format);  
              xml.write(d);
            xml.close(); 
    
        } catch (DocumentException e) {
            e.printStackTrace();
        }   
                ArrayList<User> list = new ArrayList<>();       
               String contextPath = getServletContext().getContextPath();
                String realPath = getServletContext().getRealPath("WEB-INF/user.xml");
                File xmlFile = new File(realPath);
                SAXReader reader = new SAXReader();
                try {
                    Document d = (Document) reader.read(xmlFile);

                    Element rootElement = d.getRootElement();
                    List elements = rootElement.elements();
                    System.out.println(elements.size());
                    
                    //System.out.println(selectNodes.size());
                    for (Object object : elements) {
                        User user = new User();
                        System.out.println("haha");
                        Element ele = (Element) object;
                        List attributes = ele.attributes();
                        //拿出账号这个属性
                        Attribute usernameAttribute = (Attribute) attributes.get(0);
                        Attribute passwordAttribute = (Attribute) attributes.get(1);
                        Attribute genderAttribute = (Attribute) attributes.get(2);
                        Attribute ageAttribute = (Attribute) attributes.get(3);
                        Attribute emailAttribute = (Attribute) attributes.get(4);
                        Attribute picAttribute = (Attribute) attributes.get(5);
                        Attribute uidAttribute = (Attribute) attributes.get(6);
                        
                        user.setUsername(usernameAttribute.getText());
                        user.setPassword(passwordAttribute.getText());
                        user.setGender(genderAttribute.getText());
                        user.setAge(ageAttribute.getText());
                        user.setEmail(emailAttribute.getText());
                        user.setPic(picAttribute.getText());
                        user.setUid(uidAttribute.getText());
                        
                        list.add(user);
                    }
                    
                    HttpSession session = request.getSession();
                    session.setAttribute("userList", list);
                    System.out.println(list);
                    response.setHeader("Refresh", "0;url='../pages/users.jsp'");
            
                } catch (DocumentException e) {
                    e.printStackTrace();
                }   
        }
上一篇下一篇

猜你喜欢

热点阅读