Java操作XML的一个类[原创]

  2005.04.17发表于blog.csnd.net/zxub

  这两天没什么事,又开始摆弄Java了,想写个邮件发送的东东,想到要保存什么参数,怎么保存呢?突然想到XML文件,好,就用这个。

  研究了下,感觉用dom4j好,ok,就是它了,下面把代码贴出来(修改版),随便写写,还有不足,仅供参考:

/*
 * Created on 2005-4-14 15:26:04
 * Modify no 2005-4-19 16:06:12
 */

/**
 * @author zxub
 * 
 */
import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class OperaXML {
 
 private Document document=null;
 
 public static boolean fileExist(String fileName) {
  java.io.File objFile = new java.io.File(fileName);
  if (objFile.exists()) {
   return true;
  } else {
   return false;
  }
 }
 
 public void createXMLFile(String XMLFileName, String rootName) {
  if (!fileExist(XMLFileName)) {
   this.document = DocumentHelper.createDocument();
   Element element = this.document.addElement(rootName);
   // 加入注释 element.addComment(String)
   // 加入节点 element.addElement(String);
   // 加入属性内容 element.addAttribute(NAME,VALUE);
   // 设置内容 element.setText(String);
   //System.out.println("File created!");
   saveXMLFile(XMLFileName);
  } else {
   System.out.println("File Exist!");
  }
 }
 
 public void addChild(String fatherPath,String childName, String childValue) {   
  if (this.document==null)
  {
   System.out.println("Has not get XML file, add err!");
   return;
  }
  List list = this.document.selectNodes(fatherPath);
  Iterator iter = list.iterator();
  if (iter.hasNext()) {
   Element element = (Element) iter.next();
   Element childelement = element.addElement(childName);
   childelement.setText(childValue);   
  } else {
   System.out.println("Father node does not exist!Add error!");
  }
 }
 
 public void modifyNode(String XMLFileName, String nodePath,
   String nodeValue, String newValue){
  if (this.document==null)
  {
   System.out.println("Has not get XML file, modify err!");
   return;
  }
  List list = this.document.selectNodes(nodePath);
  Iterator iter = list.iterator();
  boolean nodeExist = false;
  while (iter.hasNext()) {
   Element element = (Element) iter.next();
   if (element.getText().equals(nodeValue)) {
    element.setText(newValue);
    nodeExist = true;
   }   
  }
  if (!nodeExist) {
   System.out.println("Target node does not exist!Modify error!");
  }
 }
 
 public void saveXMLFile(String XMLFileName) {
  if (this.document==null)
  {
   System.out.println("Has not get XML file, save err!");
   return;
  }
  try {
   /** 将document中的内容写入文件中 */
   XMLWriter writer = new XMLWriter(new FileWriter(new File(
     XMLFileName)));
   writer.write(this.document);
   writer.close();
  } catch (Exception ex) {
   System.out.println(ex.getMessage());
  }
 }
 
 public void read(String XMLFileName){
  if (fileExist(XMLFileName)) {
   SAXReader reader = new SAXReader();
   try {
    this.document = reader.read(new File(XMLFileName));
   } catch (MalformedURLException e) {
    System.out.println(e.getMessage());
   } catch (DocumentException e) {
    System.out.println(e.getMessage());
   }   
  } else {
   System.out.println("XML file does not exist,read error!");
   System.exit(0);
  }  
 }
 
 public Element getRootElement() {
  if (this.document==null)
  {
   System.out.println("Has not get XML file, get root element err!");
   return null;
  }
  return this.document.getRootElement();
 }
 
 public String getNodeValue(String nodePath){
  if (this.document==null)
  {
   System.out.println("Has not get XML file, get node value err!");
   return null;
  }
  List list = this.document.selectNodes(nodePath);
  Iterator iter = list.iterator();
  boolean nodeExist = false;
  String nodeValue = null;
  if (iter.hasNext()) {
   Element element = (Element) iter.next();
   nodeValue = element.getText();
   return nodeValue;
  } else {
   System.out.println("Target node does not exist!Read node error!");
   System.exit(0);
  }
  return null;
 }
 public void close()
 {
  if (this.document==null)
  {
   System.out.println("Has not get XML file, close err!");
   return;
  }
  this.document=null;
 }
}

原文地址:https://www.cnblogs.com/zxub/p/173604.html