木子Maple先森

博客园 首页 联系 订阅 管理

//新建一个xml文档

 

private static void createXml(String path) throws UnsupportedEncodingException, FileNotFoundException, IOException {
//创建根节点
Element rootElement = DocumentHelper.createElement("student");
rootElement.addAttribute("sno", "20160810001");
//创建子节点
Element unameElement = DocumentHelper.createElement("uname");
unameElement.setText("张三");
Element pwdElement = DocumentHelper.createElement("pwd");
pwdElement.setText("123456");

rootElement.add(unameElement);
rootElement.add(pwdElement);
//创建一个文档
Document document = DocumentHelper.createDocument(rootElement);

//写出到硬盘
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter writer = new XMLWriter(new FileOutputStream(path), format);
writer.write(document);
}

posted on 2016-09-01 21:49  木子Maple先森  阅读(297)  评论(0编辑  收藏  举报