-- 作者:温柔眸子
-- 发布时间: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>
|