xml文件
[code="xml"]
<?xml version="1.0" encoding="UTF-8"?>
<info name="xiaoli">
我是小李
</info>
<info name="xiaoke">
我是小可
</info>
[/code]
我想再增加一个节点,
[code="xml"]
<info name="xiaozhang">
我是小张
</info>
[/code]
这样的情况,应该怎么处理
我的XmlUtil类
[code="java"]
package com.xatooken.callboard;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlUtil {
private SAXReader reader;
private Document document;
private File xmlFile;
private XMLWriter writer;
private List list=null;
/*初始化saxreader,并读取xml文件
*/
public XmlUtil(String fileName) throws Exception {
xmlFile = new File(fileName);
reader = new SAXReader();
document = reader.read(xmlFile);
}
/*
*
* 通过XPath读取节点,selectNodes的参数必须为全路径,本方法返回多个字符串
*
* */
public List<String> readMultiNodes(String nodeString){//nodeString格式为"/students/student",从根节点一直到当前节点
List<String> nodes=new ArrayList<String>();
List<Element> list=document.selectNodes(nodeString);
for(Element param : list) {
nodes.add(param.getTextTrim());
}
return nodes;
}
/*
*
*
*通过XPath读取节点,selectNodes的参数必须为全路径,本方法返回一个字符串
*
* */
public String readSingleNodes(String nodeString){//nodeString格式为"/students/student",从根节点一直到当前节点
Node node = document.selectSingleNode(nodeString);
return node.getText();
}
/*
*
*通过XPath读取节点属性值,selectNodes的参数必须为全路径,本方法返回一个字符串
*
*
* */
public List<String> readSingleNodesNameAttributeValue(String nodeString,String attribute){
List list = document.selectNodes(nodeString);
List nameList=new ArrayList();
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element=(Element)iter.next();
nameList.add(element.attributeValue(attribute));
}
return nameList;
}
public boolean changeCallBoardInfo (String nodeString,String AttributeValue,String path,String newInfo,String newAttributeValue){ //根据名字先读取内容
Node node = document.selectSingleNode(nodeString+"[@name='"+AttributeValue+"']");
List list = document.selectNodes(nodeString);
Iterator iter = list.iterator();
while (iter.hasNext()) {
Element element=(Element)iter.next();
if(element.attributeValue("name").equals(AttributeValue)){
element.setAttributeValue("name", newAttributeValue);
}
}
node.setText(newInfo);
try {
writer = new XMLWriter(new FileOutputStream(new File(path)));
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean addCallBoardInfo(String name,String content){
Element infos = document.getRootElement();
return true;
}
}
[/code]
测试类
[code="java"]
package com.xatooken.callboard;
public class Dom4jTest {
public static void main(String args[]){
String path="D:\Tomcat 5.5\webapps\xatooken\callboard\callBoardInfo.xml";
XmlUtil xu=null;
try {
xu = new XmlUtil(path);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//节点 属性值 路径 新内容 新属性值
//xu.changeCallBoardInfo("/infos/info","dddd",path,"我成功了","ok");
xu.addCallBoardInfo("aa", "aaaaaaaaaaaaaaaaa");
//System.out.print(xu.readSingleNodes("/infos/info[@name='根节点']"));
}
}
[/code]