转自:
下文讲述dom4j操作xml文件的方法,如下所示:
@Test public void createDocument() { //创建一篇文档 Document doc = DocumentHelper.createDocument(); //添加一个元素 Element root = doc.addElement("catalog"); //为root元素添加注释 root.addComment("An XML Catalog"); //添加标记 root.addProcessingInstruction("target", "instruction"); //创建元素 Element journalEl = new BaseElement("journal"); //添加属性 journalEl.addAttribute("title", "xml测试"); journalEl.addAttribute("publisher", "发布者"); root.add(journalEl); //添加元素 Element articleEl = journalEl.addElement("article"); articleEl.addAttribute("level", "Intermediate"); articleEl.addAttribute("date", "December-2001"); Element titleEl = articleEl.addElement("title"); //设置文本内容 titleEl.setText("Java configuration with XML Schema"); //titleEl.addText("Java configuration with XML Schema"); Element authorEl = articleEl.addElement("author"); authorEl.addElement("firstname").setText("Marcello"); authorEl.addElement("lastname").addText("Vitaletti"); //可以使用 addDocType() 方法添加文档类型说明。 doc.addDocType("catalog", null,"file://c:/Dtds/catalog.dtd"); fail(doc.getRootElement().getName()); //将xml转换成文本 fail(doc.asXML()); //写入到文件 XMLWriter output; try { output = new XMLWriter(new FileWriter(new File("file/catalog.xml"))); output.write(doc); output.close(); } catch (IOException e) { e.printStackTrace(); } }
public class GetNodes1 { /** * 将driver、url、username、password的值读取出来 */ public static void main(String[] args) throws IOException, DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read("d:/SqlMapConfig.xml"); Element driverElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='driver']"); Element urlElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='url']"); Element usernameElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='username']"); Element passwordElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']"); String driver = driverElement.attributeValue("value"); String url = urlElement.attributeValue("value"); String username = usernameElement.attributeValue("value"); String password = passwordElement.attributeValue("value"); System.out.println("driver:"+driver+",url:"+url+",username:"+username+",password:"+password); } }
public class UpdateNode { public static void main(String[] args) throws DocumentException, IOException { //读取并修改 SAXReader reader = new SAXReader(); Document document = reader.read("d:/SqlMapConfig.xml"); Element passwordElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']"); passwordElement.addAttribute("value", "123456"); //保存 OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml"); XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint()); writer.write(document); writer.close(); os.close(); } }
public class DeleteNode { public static void main(String[] args) throws DocumentException, IOException { //找到结点并删除 SAXReader reader = new SAXReader(); Document document = reader.read("d:/SqlMapConfig.xml"); Element passwordElement = (Element) document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']"); passwordElement.getParent().remove(passwordElement); //保存 OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml"); XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint()); writer.write(document); writer.close(); os.close(); } }
@Test public void removeNode() { try { Document doc = reader.read(new File("file/test.xml")); fail("comment: " + doc.selectSingleNode("//comment()")); //删除注释 doc.getRootElement().remove(doc.selectSingleNode("//comment()")); Element node = (Element) doc.selectSingleNode("//article"); //删除属性 node.remove(new DOMAttribute(QName.get("level"), "Introductory")); //删除元素 节点 node.remove(doc.selectSingleNode("//title")); //只能删除下一级节点,不能超过一级;(需要在父元素的节点上删除子元素) Node lastNameNode = node.selectSingleNode("//lastname"); lastNameNode.getParent().remove(lastNameNode); fail("Text: " + doc.selectObject("//*[text()='Ayesha']")); Element firstNameEl = (Element)doc.selectObject("//firstname"); fail("Text: " + firstNameEl.selectSingleNode("text()")); //删除text文本 //firstNameEl.remove(firstNameEl.selectSingleNode("text()")); //firstNameEl.remove(doc.selectSingleNode("//firstname/text()")); firstNameEl.remove(doc.selectSingleNode("//*[text()='Ayesha']/text()")); //删除子元素author //node.remove(node.selectSingleNode("//author")); fail(doc.asXML()); } catch (Exception e) { e.printStackTrace(); } }