Java中Dom解析XML
生活随笔
收集整理的這篇文章主要介紹了
Java中Dom解析XML
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
DOM將整個(gè)XML文件加載到內(nèi)存中,并構(gòu)建出節(jié)點(diǎn)樹(shù);應(yīng)用程序可以通過(guò)遍歷節(jié)點(diǎn)樹(shù)的方式來(lái)解析XML文件中的各個(gè)節(jié)點(diǎn)、屬性等信息;
這種方式便于對(duì)XML節(jié)點(diǎn)的添加修改等,而且解析也很方便,然后它比較耗費(fèi)內(nèi)存,解析速度也不快
sax解析: http://www.cnblogs.com/gavinYang/p/3505543.html ?
dom4j解析: http://www.cnblogs.com/gavinYang/p/3505535.html
jdom解析: http://www.cnblogs.com/gavinYang/p/3505530.html
Java代碼:
1 package com.test; 2 3 import java.io.File; 4 import java.util.ArrayList; 5 import java.util.List; 6 import javax.xml.parsers.DocumentBuilder; 7 import javax.xml.parsers.DocumentBuilderFactory; 8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element; 10 import org.w3c.dom.Node; 11 import org.w3c.dom.NodeList; 12 13 public class DomXML { 14 15 public static void main(String[] args) { 16 try { 17 File file = new File("e:/People.xml"); 18 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 19 DocumentBuilder builder = factory.newDocumentBuilder(); 20 Document document = builder.parse(file); 21 Element element = document.getDocumentElement(); 22 23 List<People> peopleList = new ArrayList<People>(); 24 NodeList peopleNodes = element.getElementsByTagName("People"); 25 for(int i=0;i<peopleNodes.getLength();i++){ 26 People people = new People(); 27 Element peopleElement = (Element) peopleNodes.item(i); 28 people.setId(peopleElement.getAttribute("id")); 29 NodeList childPeopleNodes = peopleElement.getChildNodes(); 30 for(int j=0;j<childPeopleNodes.getLength();j++){ 31 //DOM解析時(shí)候注意子節(jié)點(diǎn)前面的空格也會(huì)被解析 32 if(childPeopleNodes.item(j) instanceof Element){ 33 Element childPeopleElement = (Element) childPeopleNodes.item(j); 34 if(childPeopleElement.getNodeType()==Node.ELEMENT_NODE){ 35 if(childPeopleElement.getNodeName().equals("Name")){ 36 people.setEnglishName(childPeopleElement.getAttribute("en")); 37 people.setName(childPeopleElement.getTextContent()); 38 } 39 else if(childPeopleElement.getNodeName().equals("Age")){ 40 people.setAge(childPeopleElement.getTextContent()); 41 } 42 } 43 } 44 } 45 peopleList.add(people); 46 } 47 48 for(People people : peopleList){ 49 System.out.println(people.getId()+"\t"+people.getName()+"\t"+people.getEnglishName()+"\t"+people.getAge()); 50 } 51 52 } catch (Exception e) { 53 // TODO 自動(dòng)生成的 catch 塊 54 e.printStackTrace(); 55 } 56 57 58 } 59 }People對(duì)象:
1 package com.test; 2 3 public class People { 4 private String id; 5 private String name; 6 private String englishName; 7 private String age; 8 public String getId() { 9 return id; 10 } 11 public void setId(String id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public String getEnglishName() { 21 return englishName; 22 } 23 public void setEnglishName(String englishName) { 24 this.englishName = englishName; 25 } 26 public String getAge() { 27 return age; 28 } 29 public void setAge(String age) { 30 this.age = age; 31 } 32 33 }xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <PeopleList> 3 <People id="1"> 4 <Name en='zhangsan'>張三</Name> 5 <Age>20</Age> 6 </People> 7 <People id="2"> 8 <Name en='lisi'>李四</Name> 9 <Age>30</Age> 10 </People> 11 </PeopleList>?
轉(zhuǎn)載于:https://www.cnblogs.com/gavinYang/p/3505523.html
總結(jié)
以上是生活随笔為你收集整理的Java中Dom解析XML的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 内置对象—request
- 下一篇: ThreadGroup其实比Execut