iteye_18089 2009-04-14 11:40
浏览 195
已采纳

dom4j增加已存在的节点,怎么增加,我的一直报错

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]

  • 写回答

1条回答 默认 最新

  • wanghaolovezlq 2009-04-14 11:52
    关注

    [code="java"]
    public boolean addCallBoardInfo(String name,String content){

        Element   infos   =   document.getRootElement();   
    
        Element info = infos.addElement("info");
        info.addAttribute("name", name);
        info.addText(content);
    
        try {
            writer = new XMLWriter(new FileOutputStream(xmlFile));
            writer.write(document);
            writer.close();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    
    
        return true;
    
    }
    

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误