1、读写xml
1 package com.test.transport.service; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import org.apache.log4j.Logger; 10 import org.dom4j.Attribute; 11 import org.dom4j.Document; 12 import org.dom4j.DocumentException; 13 import org.dom4j.DocumentHelper; 14 import org.dom4j.Element; 15 import org.dom4j.io.OutputFormat; 16 import org.dom4j.io.SAXReader; 17 import org.dom4j.io.XMLWriter; 18 19 public class XMLService { 20 public static Logger log = Logger.getLogger(XMLService.class); 21 public Document getDocument(){ 22 Document doc = DocumentHelper.createDocument(); 23 doc.addComment("a simple demo "); 24 Element root = doc.addElement("Persons"); 25 Element p1 = root.addElement("Person"); 26 p1.addAttribute("name", "xiaohei"); 27 p1.addAttribute("age", "20"); 28 p1.addAttribute("sex", "male"); 29 Element p2 = root.addElement("Person"); 30 p2.addAttribute("name", "xiaobai"); 31 p2.addAttribute("age", "21"); 32 p2.addAttribute("sex", "female"); 33 return doc; 34 } 35 public void saveDocument(Document doc,String filepath){ 36 OutputFormat format = OutputFormat.createPrettyPrint(); 37 XMLWriter writer = null; 38 try { 39 writer = new XMLWriter(new FileWriter(new File(filepath)), format); 40 writer.write(doc); 41 writer.flush(); 42 writer.close(); 43 } catch (IOException e) { 44 log.error("文件错误"); 45 e.printStackTrace(); 46 } 47 } 48 public static void main(String args[]){ 49 XMLService service = new XMLService(); 50 Document doc = service.getDocument(); 51 service.saveDocument(doc, "demo.xml"); 52 } 53 }
2、增加修改方法
1 package com.test.transport.service; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import org.apache.log4j.Logger; 10 import org.dom4j.Attribute; 11 import org.dom4j.Document; 12 import org.dom4j.DocumentException; 13 import org.dom4j.DocumentHelper; 14 import org.dom4j.Element; 15 import org.dom4j.io.OutputFormat; 16 import org.dom4j.io.SAXReader; 17 import org.dom4j.io.XMLWriter; 18 19 public class XMLService { 20 public static Logger log = Logger.getLogger(XMLService.class); 21 public Document getDocument(){ 22 Document doc = DocumentHelper.createDocument(); 23 doc.addComment("a simple demo "); 24 Element root = doc.addElement("Persons"); 25 Element p1 = root.addElement("Person"); 26 p1.addAttribute("name", "xiaohei"); 27 p1.addAttribute("age", "20"); 28 p1.addAttribute("sex", "male"); 29 Element p2 = root.addElement("Person"); 30 p2.addAttribute("name", "xiaobai"); 31 p2.addAttribute("age", "21"); 32 p2.addAttribute("sex", "female"); 33 return doc; 34 } 35 public void saveDocument(Document doc,String filepath){ 36 OutputFormat format = OutputFormat.createPrettyPrint(); 37 XMLWriter writer = null; 38 try { 39 writer = new XMLWriter(new FileWriter(new File(filepath)), format); 40 writer.write(doc); 41 writer.flush(); 42 writer.close(); 43 } catch (IOException e) { 44 log.error("文件错误"); 45 e.printStackTrace(); 46 } 47 } 48 public Document editDocument(File inputFile){ 49 SAXReader saxReader = new SAXReader(); 50 Document doc = null; 51 try { 52 doc = saxReader.read(inputFile); 53 } catch (DocumentException e) { 54 log.error("读取文件错误"); 55 e.printStackTrace(); 56 } 57 List<Attribute> list = doc.selectNodes("//Persons/Person/@name"); 58 Iterator<Attribute> iterator = list.iterator(); 59 while(iterator.hasNext()){ 60 Attribute attribute = iterator.next(); 61 if(attribute.getValue().endsWith("xiaohei")){ 62 attribute.setValue("zhaodaoxiaohei"); 63 } 64 } 65 List<Element> list2 = doc.selectNodes("//Persons/Person"); 66 Iterator<Element> iterator2 = list2.iterator(); 67 while (iterator2.hasNext()) { 68 Element element = iterator2.next(); 69 element.addText("5"); 70 if(element.attributeValue("name").equals("xiaobai")){ 71 element.addElement("grade").addText("3"); 72 } 73 } 74 return doc; 75 } 76 public static void main(String args[]){ 77 XMLService service = new XMLService(); 78 Document doc = service.getDocument(); 79 service.saveDocument(doc, "demo.xml"); 80 81 Document document = service.editDocument(new File("demo.xml")); 82 service.saveDocument(document, "demo.xml"); 83 } 84 }
有关XPath详见:http://baike.baidu.com/view/307399.htm