使用DOM读取和维护XML数据
生活随笔
收集整理的這篇文章主要介紹了
使用DOM读取和维护XML数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
筆記目錄:
1.了解XML解析
2.使用DOM讀取和維護XML數據
3.使用DOM4J讀取和維護XML數據
1.DOM解析XML的步驟
?
2.使用DOM解析XML時主要使用的對象
XML中節點有三種:1.標簽節點? 2.屬性節點? 3.文本節點
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Node可以是三種節點任意一種,Element是標簽節點對象
2.1 Node對象
?
2.2 NodeList對象
?
2.3 Document對象
?
2.4 Element對象
?
注意: 1.更多API信息,請查閱JDK API
? ? ? ? ? 2.獲取子對象注意事項
?
3.DOM解析XML實例
代碼:
public class DOM解析XML {public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {//創建解析器工廠對象DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//獲得解析對象DocumentBuilder builder=factory.newDocumentBuilder();//加載要解析的XML文件Document doc=builder.parse(new File("手機信息.xml"));//構建DOM樹//操作DOM樹: 讀取,修改,添加,刪除..//getLength():獲得節點個數//Item():獲得節點集合中的第N個節點//getElementsByTagName(): 根據標簽名獲得指定的標簽[子標簽,父標簽]//getAttribute((): 獲得當前標簽的指定屬性的值//getTextContent(): 獲得當前標簽的文本內容NodeList brands=doc.getElementsByTagName("Brand");//根據標簽名獲得標簽節點for (int i = 0; i < brands.getLength(); i++) {//節點: 標簽,屬性節點,文本節點Element brand=(Element) brands.item(i);//Brand屬于標簽節點String name=brand.getAttribute("name");//el.getChildNodes();NodeList types=brand.getElementsByTagName("Type");for(int j = 0; j < types.getLength(); j++) {Element type=(Element) types.item(j);//Node向下轉型為ElementString typeName=type.getAttribute("name");//獲得type標簽的name屬性值String price=type.getTextContent();//獲得當前標簽的文本內容System.out.println("手機名:"+name+" 型號:"+typeName+" 價格:"+price);}}} }?
XML文件:
<?xml version="1.0" encoding="UTF-8" ?> <PhoneInfo><Brand name="華為"><Type name="P90">5000元</Type></Brand><Brand name="蘋果"><Type name="iPhone Z">18000元</Type><Type name="iPhone ZL">12000元</Type></Brand> </PhoneInfo>?
總結:使用DOM讀取XML其實是對Document、NodeList、Node、Element 等幾個對象的靈活運用
4.使用DOM維護XML數據(增刪改)
4.1 保存Document內容到XML文件的步驟
?
4.2 常用方法
| 方法名 | 描述 |
| Document元素.createElement(標簽名) | 創建一個標簽元素(一般會使用一個Element對象接收,方便操作元素) |
| Element元素.setAttribute("屬性名","值") | 設置標簽元素的屬性名和值 |
| Element元素.appendChild(Element元素) | 添加元素間的嵌套父子關系( 父元素.appendChild(子元素) ) |
| Element元素.removeChild(Element元素) | 刪除元素(元素不能自己刪除自己,需要用要刪除元素的父元素來刪除要刪除的元素) 要刪除元素的父元素Element對象.removeChild(要刪除的Element元素) |
| Element元素.removeAttribute("屬性名") | 刪除Element元素對應屬性名的屬性 |
| Element元素.getParentNode() | 獲取元素的父元素 |
| Element元素.setTextContent("值")/getTextContent("值") | 獲取/設置 元素的值 |
?
4.3 思路
增加標簽:使用Document樹對象創建標簽元素,把創建的標簽元素添加到對用的Element元素下,建立其嵌套父子關系
刪除標簽:標簽不能自己刪除自己,需要被刪除的標簽獲取其父標簽元素,用父標簽元素來刪除其子元素(自己)
修改:暫無
?
?
4.4 實際操作
代碼:
public class DOM解析XML {public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {//創建解析器工廠對象DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();//獲得解析對象DocumentBuilder builder=factory.newDocumentBuilder();//加載要解析的XML文件Document doc=builder.parse(new File("手機信息.xml"));//構建DOM樹//新增節點: 標簽節點,屬性節點,文本節點Element newBrand=doc.createElement("Brand");//<Brand></Brand>newBrand.setAttribute("name", "小米");//<Brand name="小米"></Brand>Element newType=doc.createElement("Type");//<Type></Type>newType.setAttribute("name", "Mi10");//<Type name="Mi10"></Type>newType.setTextContent("2999元");//<Type name="Mi10">2999元</Type>//將子標簽添加到父標簽中newBrand.appendChild(newType);//<Brand name="小米"><Type name="Mi10">2999元</Type></Brand>//修改節點: 屬性,文本newType.setAttribute("name","mi11");newType.setTextContent("1999元");//刪除節點: 標簽節點,屬性節點,文本節點newType.removeAttribute("name");newType.setTextContent("");//文本默認為""//newType.getParentNode().removeChild(newType);//刪除當前節點Node parentNode=newType.getParentNode();parentNode.removeChild(newType);//添加節點到DOM樹doc.getElementsByTagName("PhoneInfo").item(0).appendChild(newBrand);//存儲XML信息DOMSource domSource=new DOMSource(doc);//設置DOM信息Result rs=new StreamResult(new FileOutputStream(new File("新的手機信息.xml")));//信息存儲位置//格式轉換對象//創建格式轉換工廠對象TransformerFactory ttf=TransformerFactory.newInstance();//獲得格式轉換對象Transformer transformer=ttf.newTransformer();//設置轉換格式transformer.setOutputProperty(OutputKeys.VERSION, "1.0");transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");//轉換: 將DOM信息,轉到磁盤文件中transformer.transform(domSource, rs);} }?
XML文件:
<!-- 手機信息.XML --> <?xml version="1.0" encoding="UTF-8" ?> <PhoneInfo><Brand name="華為"><Type name="P90">5000元</Type></Brand><Brand name="蘋果"><Type name="iPhone Z">18000元</Type><Type name="iPhone ZL">12000元</Type></Brand> </PhoneInfo><!-- 新的手機信息.XML --> <?xml version="1.0" encoding="UTF-8" standalone="no"?><PhoneInfo><Brand name="華為"><Type name="P90">5000元</Type></Brand><Brand name="蘋果"><Type name="iPhone Z">18000元</Type><Type name="iPhone ZL">12000元</Type></Brand> <Brand name="小米"/></PhoneInfo>?
總結
以上是生活随笔為你收集整理的使用DOM读取和维护XML数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果电脑怎么装window系统安装 使用
- 下一篇: 怎么把大白菜系统装进优盘 大白菜系统如何