//得到已有的记录数,给新增的餐馆记录设定唯一的递增的id 属性 int intNum=0; Element restNum=(Element)doc.getElementsByTagName("restaurants").item(0); intNum=Integer.parseInt(restNum.getAttributeNode("num").getNodeValue());
//新增节点 Element newRestaurant=doc.createElement("restaurant");
Attr newArrId=doc.createAttribute("id"); //Attribute newArrId = new Attribute("id",String.valueOf(intNum)); textseg=doc.createTextNode(String.valueOf(intNum)); newArrId.setValue(String.valueOf(intNum)); newRestaurant.setAttributeNode(newArrId);
Element newName=doc.createElement("name"); textseg=doc.createTextNode(strName); newName.appendChild(textseg); newRestaurant.appendChild(newName);
Element newPhone=doc.createElement("phone"); textseg=doc.createTextNode(strPhone); newPhone.appendChild(textseg); newRestaurant.appendChild(newPhone);
Element newAddress=doc.createElement("address"); textseg=doc.createTextNode(strAddress); newAddress.appendChild(textseg); newRestaurant.appendChild(newAddress);