| */ public static void addElement(Document dc) { Element estu = dc.createElement("student"); Element ename = dc.createElement("name"); Element esex = dc.createElement("sex"); Element eage = dc.createElement("age"); Element escore = dc.createElement("score"); Text tname = dc.createTextNode("张三"); Text tsex = dc.createTextNode("男"); Text tage = dc.createTextNode("18"); Text tscore = dc.createTextNode("80"); estu.appendChild(ename).appendChild(tname); estu.appendChild(esex).appendChild(tsex); estu.appendChild(eage).appendChild(tage); estu.appendChild(escore).appendChild(tscore); dc.getDocumentElement().appendChild(estu); } /* |
| */ public static void updateElement(Document dc, String stname, String stscore) { NodeList nl = dc.getElementsByTagName("name"); Element e; for (int i = 0; i < nl.getLength(); i++) { e = (Element) nl.item(i); if (e.getFirstChild().getNodeValue().equals(stname)) { Node n = e.getParentNode(); NodeList nd = n.getChildNodes(); for (int j = 0; j < nd.getLength(); j++) { Node node = nd.item(j); if (node.getNodeName().equals("score")) { node.getFirstChild().setNodeValue(stscore); } } } } } /* |

