[置顶] 欢迎您。今天是2022年11月30日。

在项目中,我们很多都用到了xml文件,无论是参数配置还是与其它系统的数据交互。今天就来讲一下Java 中使用dom4j来操作XML文件。

1. 将XML文件的内容转化为String
public static String doc2String(Document document) {
  String s = "";
  try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputFormat format = new OutputFormat("  ", true, "GB2312");
    XMLWriter writer = new XMLWriter(out, format);
    writer.write(document);
    s = out.toString("GB2312");
  } catch(Exception ex) {
    ex.printStackTrace();
  }
  return s; 
}
2. 将符合XML格式的String 转化为XML Document
public static Document string2Document(String s) {
  Document doc = null;
  try {
    doc = DocumentHelper.parseText(s);
  } catch(Exception ex) {
    ex.printStackTrace();
  }
  return doc;
}
3. 将Document对象保存为一个xml文件到本地
public static boolean doc2XmlFile(Document document, String filename) {
  boolean flag = true;
  try {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("GB2312");
    XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)),format);
    writer.write(document);
    writer.close();
  } catch(Exception ex) {
    flag = false;
    ex.printStackTrace();
  }
  return flag;
}
4. 将xml格式的字符串保存为本地文件,如果字符串格式不符合xml规则,则返回失败
public static boolean string2XmlFile(String str, String filename) {
  boolean flag = true;
  try {
    Document doc =  DocumentHelper.parseText(str);
    flag = doc2XmlFile(doc, filename);
  } catch(Exception ex) {
    flag = false;
    ex.printStackTrace();
  }
  return flag;
}
5. 载入一个xml文档
public static Document load(String filename) {
  Document document = null;
  try {
    SAXReader saxReader = new SAXReader();
    document = saxReader.read(new File(filename));
  } catch(Exception ex) {
    ex.printStackTrace();
  }  
  return document; 
}
6. 演示String保存为xml文件
public void xmlWriteDemoByString() {
  String s = "<test>test</test>";
  string2XmlFile(s, "xmlWriteDemoByString.xml");   
  string2XmlFile(s, "classes/xmlWriteDemoByString.xml");  
}
7. 演示手动创建一个Document,并保存为XML文件
public void xmlWriteDemoByDocument() {
  Document document = DocumentHelper.createDocument();
  Element configElement = document.addElement("config");
  configElement.addComment("东电ftp配置");
  Element ftpElement = configElement.addElement("ftp");
  ftpElement.addAttribute("name", "DongDian");
  Element hostElement = ftpElement.addElement("ftp-host");
  hostElement.setText("127.0.0.1");
  (ftpElement.addElement("ftp-port")).setText("21");
  (ftpElement.addElement("ftp-user")).setText("cxl");
  (ftpElement.addElement("ftp-pwd")).setText("longshine");
  ftpElement.addComment("ftp最多尝试连接次数");
  (ftpElement.addElement("ftp-try")).setText("50");
  ftpElement.addComment("ftp尝试连接延迟时间");
  (ftpElement.addElement("ftp-delay")).setText("10");    
  doc2XmlFile(document, "classes/xmlWriteDemoByDocument.xml"); 
}
8. 演示读取文件的具体某个节点的值
public static void xmlReadDemo() {
  Document doc = load("classes/xmlWriteDemoByDocument.xml");
  List list = doc.selectNodes("/config/ftp");
  Iterator it = list.iterator();
  while(it.hasNext()) {   
    Element ftpElement = (Element)it.next();
    System.out.println("ftp_name="+ ftpElement.attribute("name").getValue());
  }
  list = doc.selectNodes("/config/ftp/@name");
  it = list.iterator();
  while(it.hasNext()) {   
    Attribute attribute = (Attribute)it.next();
    System.out.println("@name="+ attribute.getValue());
  }
  list = doc.selectNodes("/config/ftp/ftp-host");
  it = list.iterator();
  Element hostElement = (Element)it.next();
  System.out.println("DongDian's ftp_host="+ hostElement.getText()); 
}
9. 修改或删除某个值或属性
ftpElement.remove(hostElement);
ftpElement.remove(nameAttribute);
hostElement.setText("192.168.0.1");
nameAttribute.setValue("ChiFeng");
您可能也喜欢
二维码
摄像头