public void insertElementNode(String fileName, String nodeName,
String newElementName) {
document = parserXml(fileName);
NodeList nodeList = document.getElementsByTagName(nodeName);
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = document.createElement(newElementName);
nodeList.item(i).appendChild(element);
}
createXml(fileName);
}
public void insertAttrNode(String fileName, String nodeName,
String newAttrName, String attrValue) {
document = parserXml(fileName);
NodeList nodeList = document.getElementsByTagName(nodeName);
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) (nodeList.item(i));
element.setAttribute(newAttrName, attrValue);
}
createXml(fileName);
}
public void insertTextNode(String fileName, String nodeName, String textNode) {
document = parserXml(fileName);
NodeList nodeList = document.getElementsByTagName(nodeName);
for (int i = 0; i < nodeList.getLength(); i++) {
nodeList.item(i).appendChild(document.createTextNode(textNode));
}
createXml(fileName);
}
public void deleteElementNode(String fileName, String nodeName) {
document = parserXml(fileName);
NodeList nodeList = document.getElementsByTagName(nodeName);
while (nodeList.getLength() > 0) {
nodeList.item(0).getParentNode().removeChild(nodeList.item(0));
nodeList = document.getElementsByTagName(nodeName);
}
createXml(fileName);
}
public void updateNode(String fileName, String nodeName, String attrName,
String newAttrValue) {
document = parserXml(fileName);
NodeList nodeList = document.getElementsByTagName(nodeName);
for (int i = 0; i < nodeList.getLength(); i++) {
Element node = (Element) nodeList.item(i);
node.setAttribute(attrName, newAttrValue);
}
createXml(fileName);
}
private Document parserXml(String fileName) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
System.out.println("-----------------------------------" + "解析完毕"
+ "----------------------------------------");
return document;
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println(e.getMessage());
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
return document;
}
private void createXml(String fileName) {
try {
/** 将document中的内容写入文件中 */
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new FileOutputStream(
fileName));
transformer.transform(source, result);
System.out.println("--------------------------------"
+ "更新 XML文件成功" + "-------------------------------------");
} catch (Exception exception) {
System.out.println("更新" + fileName + "出错:" + exception);
exception.printStackTrace();
}
}
}
最后程序提供的接口与说明如下
?
?
/**
* @author GuoDi and ZhangWen
*
*/
public interface NodeInfoInterface {
/**
* XML文档 插元素入节点
* @param time 时间
* @param nodeName 标签名
* @param newElementName 新标签
*/
public void insertElementNode(String time, String nodeName,String newElementName);
/**
* @param time 时间
* @param nodeName 标签名
* @param newAttrName 新属性名
* @param attrValue 新属性值
* XML文档 插入属性节点
*/
public void insertAttrNode(String time,String nodeName,String newAttrName,String attrValue);
/**
* @param time 时间
* @param nodeName 标签名
* @param textNode 文本
* XML文档 插入文本节点
*/
public void insertTextNode(String time,String nodeName,String textNode);
/**
* @param time 时间
* @param nodeName 标签名
* XML文档 删除所有对应元素节点
*/
public void deleteElementNode(String time,String nodeName);
/**
* @param time 时间
* @param nodeName 标签名
* @param newAttrName 新属性名
* @param attrValue 新属性值
* XML文档 修改属性节点内容
*/
public void updateNode(String time,String nodeName,String newAttrName,String attrValue);
}
?
|