zoukankan      html  css  js  c++  java
  • dom4j简单实例

    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

  • 相关阅读:
    动态类(类工厂)总结
    m3u8字段意义解析
    libevent+bufferevent总结
    g++编译总结
    ffplay 参数说明分享
    PHP——最新号码归属地数据库
    PHP——汉字完美转为ASCII码
    PHP——json_encode转码保留中文
    workerman——报错
    PHP——封装Curl请求方法支持POST | DELETE | GET | PUT 等
  • 原文地址:https://www.cnblogs.com/ikuman/p/2800872.html
Copyright © 2011-2022 走看看