import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.io.IOException; public class Test1 { public static void main(String[] args) { Test1 test=new Test1(); test.creatObject(); test.show(); System.out.println("================="); test.change("三星","三丧"); test.change("三丧","Note20","Note22"); test.show(); System.out.println("================="); test.add("小米"); test.show(); System.out.println("================="); test.add("小米","mi11"); test.show(); System.out.println("================="); // test.delete("小米"); test.save(); } private Document document; public void creatObject(){ DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newDefaultInstance(); try { DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); document=documentBuilder.parse("src\\test1\\PhoneInfo.xml"); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } } public void show(){ NodeList nodeList=document.getElementsByTagName("Brand"); System.out.println("Brand节点数:"+nodeList.getLength()); for (int i = 0; i < nodeList.getLength(); i++) { Element element=(Element) nodeList.item(i); System.out.println(element.getAttribute("name")); NodeList nodeList1=element.getChildNodes(); System.out.println("name节点数:"+nodeList1.getLength()); for (int j = 0; j <nodeList1.getLength(); j++) { Node node=nodeList1.item(j); if (node.getNodeType()==Node.ELEMENT_NODE){ Element element1=(Element) node; System.out.println(element1.getAttribute("name")); } } } } public void save(){ TransformerFactory transformerFactory=TransformerFactory.newDefaultInstance(); try { Transformer transformer=transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8"); DOMSource domSource=new DOMSource(document); StreamResult streamResult=new StreamResult(new File("src\\test1\\newPhoneInfo.xml")); transformer.transform(domSource,streamResult); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public void change(String value,String changes){ NodeList nodeList=document.getElementsByTagName("Brand"); for (int i = 0; i < nodeList.getLength() ; i++) { Element element=(Element) nodeList.item(i); if (element.getAttribute("name").equals(value)){ element.setAttribute("name", changes); } } } public void change(String value,String sonValue,String changes){ NodeList nodeList=document.getElementsByTagName("Brand"); for (int i = 0; i < nodeList.getLength() ; i++) { Element element=(Element) nodeList.item(i); if (element.getAttribute("name").equals(value)){ NodeList nodeList1=element.getChildNodes(); for (int j = 0; j <nodeList1.getLength() ; j++) { Node node=nodeList1.item(j); if (node.getNodeType()==Node.ELEMENT_NODE){ Element element1=(Element) node; if (element1.getAttribute("name").equals(sonValue)){ element1.setAttribute("name", changes); } } } } } } public void add(String name){ Element element=document.createElement("Brand"); element.setAttribute("name",name); NodeList PhoneInfosNode = document.getElementsByTagName("PhoneInfo"); for (int i = 0; i < PhoneInfosNode.getLength(); i++) { Node PhoneInfos = PhoneInfosNode.item(i); if (PhoneInfos.getNodeType() == Node.ELEMENT_NODE) { PhoneInfos.appendChild(element); } } } public void add(String name,String addName){ Element element=document.createElement("Type"); element.setAttribute("name",name); NodeList nodeList=document.getElementsByTagName("Brand"); for (int i = 0; i < nodeList.getLength() ; i++) { Element element1=(Element) nodeList.item(i); if (element1.getAttribute("name").equals(addName)){ element1.appendChild(element); } } NodeList PhoneInfosNode = document.getElementsByTagName("PhoneInfo"); for (int i = 0; i < PhoneInfosNode.getLength(); i++) { Node PhoneInfos = PhoneInfosNode.item(i); if (PhoneInfos.getNodeType() == Node.ELEMENT_NODE) { PhoneInfos.appendChild(element); } } } public void delete(String name){ NodeList nodeList=document.getElementsByTagName("Brand"); for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); String brandAttribute = element.getAttribute("name"); if(brandAttribute.equals(name)){ document.getElementsByTagName("PhoneInfo").item(0).removeChild(element); } } } }
//xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?><PhoneInfo> <Brand name="苹果"> <Type name="Iphone4"/> <Type name="Iphone5"/> <Type name="Iphone6"/> </Brand> <Brand name="三丧"> <Type name="Note10"/> <Type name="Note22"/> <Type name="Note21"/> </Brand> <Brand name="华为"> <Type name="Mate10"/> <Type name="Mate20"/> <Type name="Mate30"/> </Brand> <Type name="mi11"/></PhoneInfo>