以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  关于dom,更新xml文件,高手求救!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=31012)


--  作者:温柔眸子
--  发布时间:4/21/2006 12:05:00 PM

--  关于dom,更新xml文件,高手求救!
我写了下面这段程序,插入好用,但更新没有实现,请高手指点

 public void toWrite(Information information) {
  Node first = document.getFirstChild();  
  //update data
  String id = information.getId();
  if (id != null && !id.equals("")){   
   NodeList list = first.getChildNodes();
   Node nodeitm1 = list.item(Integer.parseInt(id)-1);
   //nodeitm1.getAttributes().getNamedItem("id").setNodeValue(String.valueOf(Integer.parseInt(id)));
   NodeList workshoplist = nodeitm1.getChildNodes();
   for (int j = 0; j < workshoplist.getLength(); j++) {
    Node nodeitm = workshoplist.item(j);
       
    if (nodeitm.getNodeName().equals("Title")) {
     nodeitm.getFirstChild().setNodeValue(information.getTitle());
    }
    if (nodeitm.getNodeName().equals("Content")) {
     nodeitm.getFirstChild().setNodeValue(information.getContent());
    }
    if (nodeitm.getNodeName().equals("InputDate")) {
     nodeitm.getFirstChild().setNodeValue(information.getInputDate());
    }
   }
  }else{
   String next_id = first.getAttributes().getNamedItem("nextid").getNodeValue();
   first.getAttributes().getNamedItem("nextid").setNodeValue(
     String.valueOf(Integer.parseInt(next_id) + 1));
   Element root = document.createElement("WorkShop");
   root.setAttribute("id", next_id);
   Element title = document.createElement("Title");
   title.appendChild(document.createTextNode(information.getTitle()));
   root.appendChild(title);
   Element content = document.createElement("Content");
   content.appendChild(document.createTextNode(information.getContent()));
   root.appendChild(content);
   Element inputDate = document.createElement("InputDate");
   inputDate.appendChild(document.createTextNode(information
     .getInputDate()));
   root.appendChild(inputDate);
   first.appendChild(root);
  }
 }

xml文件如下.

<?xml version="1.0" encoding="GB2312"?>
<WorkShops nextid="5">
<WorkShop id="1">
<Title>1222uuuuuuuuuu</Title>
<Content>12222</Content>
<InputDate>Thu Apr 20 16:57:30 CST 2006</InputDate>
</WorkShop>
<WorkShop id="2">
<Title>1</Title>
<Content>1sssss</Content>
<InputDate>Thu Apr 20 16:59:53 CST 2006</InputDate>
</WorkShop>
<WorkShop id="3">
<Title>limu</Title>
<Content>limu</Content>
<InputDate>Thu Apr 20 16:36:02 CST 2006</InputDate>
</WorkShop>
<WorkShop id="4">
<Title>1</Title>
<Content>1</Content>
<InputDate>Thu Apr 20 16:37:15 CST 2006</InputDate>
</WorkShop>
</WorkShops>



--  作者:fumeiyu
--  发布时间:4/26/2006 8:48:00 PM

--  
save~`
--  作者:liuzhidan
--  发布时间:5/17/2006 6:01:00 PM

--  
楼主 生成的xml   新加的节点又没有命名空间?
--  作者:sd3377312
--  发布时间:5/30/2006 5:32:00 PM

--  
有人知道吗?
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms