publicvoidadd(){//创建Brand元素
Element element = document.createElement("Brand");
element.setAttribute("name","三星");//创建Type元素
Element typee = document.createElement("Type");
typee.setAttribute("name","Note10");//将Type元素放入Brand中
element.appendChild(typee);//将Brand元素存入根节点中
document.getElementsByTagName("PhoneInfo").item(0).appendChild(element);this.saveXML("new.xml");}
修改节点
publicvoidupdate(){
NodeList brands = document.getElementsByTagName("Brand");for(int i =0; i < brands.getLength(); i++){
Node brand = brands.item(i);
Element eleBrand =(Element) brand;if(eleBrand.getAttribute("name").equalsIgnoreCase("三星")){
eleBrand.setAttribute("name","OPPO");}}this.saveXML("new.xml");}
删除节点
publicvoiddelete(){
NodeList brands = document.getElementsByTagName("Brand");for(int i =0; i < brands.getLength(); i++){
Node brand = brands.item(i);
Element eleBrand =(Element) brand;if(eleBrand.getAttribute("name").equalsIgnoreCase("OPPO")){
eleBrand.getParentNode().removeChild(eleBrand);}}this.saveXML("new.xml");}
publicvoidaddPhoneInfo(){
Element root = doc.getRootElement();//创建一个Brand节点
Element brandEle = root.addElement("Brand");
brandEle.addAttribute("name","三星");//创建Type节点
Element TypeEle = brandEle.addElement("Type");
TypeEle.addAttribute("name","Note10");this.savexml("new收藏.xml");}
修改节点
publicvoidupdatePhoneInfo(){
Element root = doc.getRootElement();
Iterator itBrand = root.elementIterator();while(itBrand.hasNext()){
Element brandEle =(Element) itBrand.next();if(brandEle.attributeValue("name").equals("三星")){
brandEle.setAttributeValue("name","SUMSUNG");}}this.savexml("new收藏.xml");}
删除节点
publicvoiddeletePhoneInfo(){
Element root = doc.getRootElement();
Iterator itBrand = root.elementIterator();while(itBrand.hasNext()){
Element brandEle =(Element) itBrand.next();if(brandEle.attributeValue("name").equals("三星")){
brandEle.getParent().remove(brandEle);}}this.savexml("new收藏.xml");}