首页 > 代码库 > XML使用DOM操作
XML使用DOM操作
DOM 文档对象模型
基于XML文档树结构解析
适用于多次访问的xml文档
特点:比较消耗资源
1).Document对象
NodeList getElementsByTagName(String name):指定节点名称的元素集合,返回NodeList对象
Element getDocumentElement():
2).NodeList对象
指包含了一个或者多个节点(Node)的列表.
getLength():返回列表的长度.
item(int index):返回指定位置的Node对象
3).Node对象
getChildNodes():包含此节点的所有子节点的NodeList
getFirstChild():如果节点存在子节点,则返回第一个子节点
getLastChild():如果节点存在子节点,则返回最后一个子节点
getNextSibling():返回在DOM树中这个节点的下一个兄弟节点
getPreviousSibling():返回在DOM树中这个节点的上一个兄弟节点
getNodeName():返回节点的名称
getNodeValue():返回节点的值
getNodeType():返回节点的类型
4).Element对象
Node的子对象
getAttribute(String attributename):返回标签中给定属性名称的属性的值
getElementsByTagName(String name):返回具有给定标记名称的所有后代Elements的NodeList
if (typeNode.getNodeType()==Node.ELEMENT_NODE):判断是否为元素节点
//添加子节点 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); Document document = db.parse("src/com/xml/PhoneInfo.xml"); //创建Brand节点 Element element = document.createElement("Brand"); element.setAttribute("name", "MOTO"); //创建Type节点 Element typElement = document.createElement("Type"); typElement.setAttribute("name", "摩托罗拉"); //创建Item节点 Element itemeElement = document.createElement("Item"); //添加父子关系 typElement.appendChild(itemeElement); element.appendChild(typElement); Element phoneElement = (Element)document.getElementsByTagName("PhoneInfo").item(0); phoneElement.appendChild(element); //保存xml文件 TransformerFactory tff = TransformerFactory.newInstance(); try { Transformer transformer = tff.newTransformer(); DOMSource domSource = new DOMSource(document); //设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); StreamResult result = new StreamResult(new FileOutputStream("src/com/xml/PhoneInfo.xml")); //把DOM树转换成XML文件 transformer.transform(domSource, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
//修改子节点 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); Document document = db.parse("src/com/xml/PhoneInfo.xml"); NodeList nodeList = document.getElementsByTagName("Type"); for (int i=0;i<nodeList.getLength();i++){ Element element = (Element)nodeList.item(i); String brandName = element.getAttribute("name"); System.out.println("修改前:"+brandName); if ("iphone4".equals(brandName)){ element.setAttribute("name", "iPad"); } } //保存xml文档 TransformerFactory tff = TransformerFactory.newInstance(); try { Transformer transformer = tff.newTransformer(); DOMSource domSource = new DOMSource(document); //设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); StreamResult result = new StreamResult(new FileOutputStream("src/com/xml/PhoneInfo.xml")); //把DOM树转换成XML文件 transformer.transform(domSource, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
//删除节点 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse("src/com/xml/PhoneInfo.xml"); NodeList list = document.getElementsByTagName("Brand"); for (int i=0;i<list.getLength();i++){ Element element = (Element)list.item(i); String brandName = element.getAttribute("name"); if ("MOHAHA".equals(brandName)){ element.getParentNode().removeChild(element); } } //保存文件 TransformerFactory tff = TransformerFactory.newInstance(); try { Transformer transformer = tff.newTransformer(); DOMSource domSource = new DOMSource(document); //设置编码类型 transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); StreamResult result = new StreamResult(new FileOutputStream("src/com/xml/PhoneInfo.xml")); //把DOM树转换成XML文件 transformer.transform(domSource, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
设置缩进:
①
TransformerFactory tff = TransformerFactory.newInstance();
tff.setAttribute("indent-number", 4);
②
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream("src/com/xml/Phone.xml"),"utf-8"));
XML使用DOM操作