XML操作举例→实现用户登录
XmlUtils.java package cn.zyw.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; //3 public class XmlUtils { private static String filepath; static{ filepath=XmlUtils.class.getClassLoader().getResource("users.xml").getPath(); filepath=filepath.replace("%20"," "); } public static Document getDocument() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(new File(filepath)); return document; } public static void write2Xml(Document document) throws Exception{ OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream(filepath),format ); writer.write( document ); writer.close(); } }
有时我们想做做一个小的项目需要保存一定的数据,如果用数据库当然是可以的。但是总觉得有点浪费资源。所以今天就学习了一点Xml总结如下: 1:Xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <users> <user id="234343434" username="aaa" password="123" email="aa@sina.com" birthday="1900-09-18" nickname="强子" /> </users> 2:操作Xml文件的工具类 XmlUtils.java package cn.zyw.utils; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; //3 public class XmlUtils { private static String filepath; static{ filepath=XmlUtils.class.getClassLoader().getResource("users.xml").getPath(); filepath=filepath.replace("%20",format ); writer.write( document ); writer.close(); } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Flex:备忘->Flex 自定义组件外观/九宫格/圆角效果
- Ruby – Thor首先执行特定任务
- ruby-on-rails – Ruby on Rails NameError:未初始化的常量
- Bad version number in .class file
- logback logback.xml常用配置详解(三) <filter>
- Study Flex《JavaScript检测flash player的版本号》
- (四十三)c#Winform自定义控件-Listview
- IBM Flex System刀片服务器做虚拟化(刀片网络交换机调试部
- 正则表达式 – 正则表达式不捕获我的网站上的文本
- ruby-on-rails – 如何在Rails中更快地使has_secure_passwo