import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import com.demos.util.HelpClass;
 public class ToolXml {
 /** *//**
* 创建只有根节点的xml文件
*
* @param rootStr
* 根节点名称
* @param xmlPath
* 将xml存放的位置及xml文件名称,如
* "D:\\TalkTimestamps.xml"(如果存放在根目录下,只要指定xml的文件名即可
* ,即"TalkTimestamps.xml")
*/
 public static void CreateXmlDoc(String rootStr, String xmlPath) {
// 创建根节点
Element root = new Element(rootStr);
// 根节点添加到文档中
Document doc = new Document(root);
// 将xml文件输出
OutputXmlDoc(doc, xmlPath);
}
 /** *//**
* 将Document写入到指定目录下的xml文件中
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如
* "D:\\TalkTimestamps.xml"(如果存放在根目录下,只要指定xml的文件名即可)
*/
 public static void OutputXmlDoc(Document doc, String xmlPath) {
 try {
// 创建XML文件输出流
XMLOutputter xmlOutput = new XMLOutputter();
// 设置所创建的XML文档的格式
Format format = Format.getPrettyFormat();
xmlOutput.setFormat(format);
// 创建文件输出流
FileWriter writer = new FileWriter(xmlPath);
// 将生成的XML文档写入到"D:\TalkTimestamps.xml"文件中
xmlOutput.output(doc, writer);
writer.close();
 } catch (FileNotFoundException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
}
}
 /** *//**
* 得到xml文件
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如
* "D:\\TalkTimestamps.xml"(如果存放在根目录下,只要指定xml的文件名即可)
* @return
*/
 public static Document getXmlDoc(String xmlPath) {
// 创建用来解析XML文档的SAXBuilder类的builder对象,并使用该对象中的build方法处理XML文件
SAXBuilder builder = new SAXBuilder();
Document doc = null;
 try {
doc = builder.build(new File(xmlPath));
 } catch (JDOMException e) {
e.printStackTrace();
 } catch (IOException e) {
e.printStackTrace();
}
return doc;
}
 public static boolean isExists(String xmlPath) {
System.out.println("");
File f = new File(xmlPath);
return f.exists();
}
 /** *//**
* 增加结点
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如"D:\\TalkTimestamps.xml"(如果存放在根目录下,
* 只要指定xml的文件名即可)
* @param nodeName
* 节点名
* @param paramsList
* 节点所含的子节点(key,value)
*/
 public static void addXmlElement(String xmlPath, Element elem) {
Document doc = getXmlDoc(xmlPath);
// 得到父节点
Element root = doc.getRootElement();
// 给父节点root添加elem子节点
root.addContent(elem);
// 输出到xml文件
OutputXmlDoc(doc, xmlPath);
}
 /** *//**
* 增加结点
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如"D:\\TalkTimestamps.xml"(如果存放在根目录下,
* 只要指定xml的文件名即可)
* @param nodeName
* 结点名称
* @param paramsList
* 子节点(key,value)
* @param attrsList
* 属性(key,value)
*/
public static void addXmlElement(String xmlPath, String nodeName,
ArrayList<HashMap<String, String>> paramsList,
 ArrayList<HashMap<String, String>> attrsList) {
Document doc = getXmlDoc(xmlPath);
// 得到父节点
Element root = doc.getRootElement();
Element elem = CreateXmlElement(nodeName, paramsList, attrsList);
// 给父节点root添加elem子节点
root.addContent(elem);
// 输出到xml文件
OutputXmlDoc(doc, xmlPath);
}
 /** *//**
* 创建新结点
*
* @param nodeName
* 结点名称
* @param paramsList
* 子节点信息
* @param attrsList
* 属性信息(key,value)
* @return
*/
public static Element CreateXmlElement(String nodeName,
ArrayList<HashMap<String, String>> paramsList,
 ArrayList<HashMap<String, String>> attrsList) {
Element node = new Element(nodeName);
if (paramsList != null)
 for (HashMap<String, String> param : paramsList) {
// 为节点添加子节点
node.addContent(new Element(param.get("key")).setText(param
.get("value")));
}
if (attrsList != null)
 for (HashMap<String, String> attr : attrsList) {
// 声明节点属性 并赋值
Attribute att = new Attribute(attr.get("key"), attr.get(attr
.get("value")));
// 为节点添加属性
node.setAttribute(att);
}
return node;
}
 /** *//**
* 修改结点内容
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如"D:\\TalkTimestamps.xml"(如果存放在根目录下,
* 只要指定xml的文件名即可)
* @param keys
* 修改Element的关键字,keys.get(i).get("key")为与子节点对应的节点名,keys.get(i).
* get("value")为与子节点对应的节点值
* @param changeList
* 修改Element的内容,changeList.get(i).get("key")为与子节点对应的节点名,
* changeList.get(i).get("value")为与子节点对应的节点值
*/
public static void ModifyXmlElement(String xmlPath,
ArrayList<HashMap<String, String>> keys,
 ArrayList<HashMap<String, String>> changeList) {
Document doc = getXmlDoc(xmlPath);
Element root = doc.getRootElement();
// 获得XML文档根节点元素下的全部子节点元素
List<Element> elements = root.getChildren();
 for (Element elem : elements) {
boolean flag = true;
 for (HashMap<String, String> key : keys) {
Element keyElement = elem.getChild(key.get("key"));
 if (!keyElement.getText().equals(key.get("value"))) {
flag = false;
break;
}
}
 if (flag) {
 for (HashMap<String, String> change : changeList) {
elem.getChild(change.get("key")).setText(
change.get("value"));
}
break;
}
}
OutputXmlDoc(doc, xmlPath);
}
 /** *//**
* 修改结点内容(只修改一项)
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如"D:\\TalkTimestamps.xml"(如果存放在根目录下,
* 只要指定xml的文件名即可)
* @param key
* 存放要更改结点的key, HashMap<String, String>的key为"key"和"value"
* @param change
* 存放要更改的内容, HashMap<String, String>的key为"key"和"value"
*/
public static void ModifyXmlElement(String xmlPath,
ArrayList<HashMap<String, String>> keys,
 HashMap<String, String> change) {
Document doc = getXmlDoc(xmlPath);
Element root = doc.getRootElement();
// 获得XML文档根节点元素下的全部子节点元素
List<Element> elements = root.getChildren();
 for (Element elem : elements) {
boolean flag = true;
 for (HashMap<String, String> key : keys) {
Element keyElement = elem.getChild(key.get("key"));
 if (keyElement == null) {
flag = false;
break;
}
 if (!keyElement.getText().equals(key.get("value"))) {
flag = false;
break;
}
}
 if (flag) {
elem.getChild(change.get("key")).setText(change.get("value"));
}
}
OutputXmlDoc(doc, xmlPath);
}
 /** *//**
* 删除结点
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如"D:\\TalkTimestamps.xml"(如果存放在根目录下,
* 只要指定xml的文件名即可)
* @param key
* 删除Element的关键字,keys.get(i).get("key")为与子节点对应的节点名,keys.get(i).
* get("value")为与子节点对应的节点值
*/
public static void DeleteXmlElement(String xmlPath,
 HashMap<String, String> key) {
Document doc = getXmlDoc(xmlPath);
Element root = doc.getRootElement();
List<Element> elements = root.getChildren();
ArrayList<Element> remove = new ArrayList<Element>();
 for (int i = 0; i < elements.size(); i++) {
Element elem = elements.get(i);
// 根据子节点名得到节点的子节点
Element keyElement = elem.getChild(key.get("key"));
 if (keyElement.getText().equals(key.get("value"))) {
remove.add(elem);
}
}
 for(int i = 0; i < remove.size(); i++) {
// 删除节点elem
root.removeContent(remove.get(i));
}
OutputXmlDoc(doc, xmlPath);
}
 /** *//**
* 得到指定结点的值
*
* @param xmlPath
* 将xml存放的位置及xml文件名称,如"D:\\TalkTimestamps.xml"(如果存放在根目录下,
* 只要指定xml的文件名即可)
* @param keys
* 查找Element的关键字,keys.get(i).get("key")为与子节点对应的节点名,keys.get(i).
* get("value")为与子节点对应的节点值
* @param paramsList
* 指定要返回的内容的节点名
* @return ArrayList<String>的值为Element的内容,其顺序对应于paramsList的key值
*/
public static ArrayList<String> getElementContent(String xmlPath,
ArrayList<HashMap<String, String>> keys,
 ArrayList<String> paramsList) {
Document doc = getXmlDoc(xmlPath);
Element root = doc.getRootElement();
List<Element> elements = root.getChildren();
ArrayList<String> result = new ArrayList<String>();
 for (Element elem : elements) {
boolean flag = true;
 for (HashMap<String, String> key : keys) {
Element keyElement = elem.getChild(key.get("key"));
 if (keyElement == null) {
flag = false;
break;
}
 if (!keyElement.getText().equals(key.get("value"))) {
flag = false;
break;
}
}
 if (flag) {
 for (String param : paramsList) {
result.add(elem.getChildText(param));
}
}
}
return result;
}
 public static void main(String[] args) {
String xmlPath = "user.xml";
//创建只有根节点的xml文件
// ToolXml.CreateXmlDoc("Users", xmlPath);
//
// //添加节点
// ToolXml.testAddElement(xmlPath, "1111", "2222");
// ToolXml.testAddElement(xmlPath, "1111", "3333");
//
// //修改节点内容
// ToolXml.testModifyElement(xmlPath, "1111", "2222");
//删除节点
ToolXml.testDeleteElement(xmlPath, "3333");
//得到节点信息
// ToolXml.testGetElement(xmlPath, "22222");
}
 /** *//**
* 测试删除节点
* @param xmlPath
* @param userid
*/
 public static void testDeleteElement(String xmlPath, String userid) {
HashMap<String, String> key = new HashMap<String, String>();
key.put("key", "userid");
key.put("value", userid);
ToolXml.DeleteXmlElement(xmlPath, key);
}
 /** *//**
* 测试添加节点
* @param xmlPath
* @param otheruserid
* @param userid
*/
 public static void testAddElement(String xmlPath, String otheruserid, String userid) {
ArrayList<HashMap<String, String>> paramsList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> param = new HashMap<String, String>();
param.put("key", "otheruserid");
param.put("value", otheruserid);
paramsList.add(param);
param = new HashMap<String, String>();
param.put("key", "userid");
param.put("value", userid);
paramsList.add(param);
param = new HashMap<String, String>();
param.put("key", "timestamp");
param.put("value", new Date().toString());
paramsList.add(param);
addXmlElement(xmlPath, "User", paramsList, null);
}
 /** *//**
* 测试修改节点信息
* @param xmlPath
* @param otheruserid
* @param userid
*/
 public static void testModifyElement(String xmlPath, String otheruserid, String userid) {
ArrayList<HashMap<String, String>> keys = new ArrayList<HashMap<String,String>>();
HashMap<String, String> param = new HashMap<String, String>();
param.put("key", "otheruserid");
param.put("value", otheruserid);
keys.add(param);
param = new HashMap<String, String>();
param.put("key", "userid");
param.put("value", userid);
keys.add(param);
ArrayList<HashMap<String, String>> changeList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> change = new HashMap<String, String>();
change.put("key", "userid");
change.put("value", "22222");
changeList.add(change);
change = new HashMap<String, String>();
change.put("key", "otheruserid");
change.put("value", "222222");
changeList.add(change);
change = new HashMap<String, String>();
change.put("key", "timestamp");
change.put("value", HelpClass.Date2StringAccurate(new Date()));
changeList.add(change);
ToolXml.ModifyXmlElement(xmlPath, keys, changeList);
}
 /** *//**
* 测试得到节点信息
* @param xmlPath
* @param userid
*/
 public static void testGetElement(String xmlPath, String userid) {
ArrayList<HashMap<String, String>> keys = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("key", "userid");
map.put("value", userid);
keys.add(map);
ArrayList<String> paramsList = new ArrayList<String>();
paramsList.add("userid");
paramsList.add("otheruserid");
paramsList.add("timestamp");
ArrayList<String> result = null;
 if (isExists(xmlPath)) {
result = ToolXml.getElementContent(xmlPath, keys, paramsList);
System.out.println("result: "+result.toString());
if(result.size() != 0)
for (String s : result)
System.out.println(s);
else
System.out.println("getElement returns empty");
 } else {
System.out.println("xml file is not exists");
}
}
}

|