DOM解析是把整個需要解析的xml文件暫存在內存中。
需要解析的XML文檔:
<?
xml version="1.0" encoding="UTF-8"
?>
< persons >
< person id ="23" >
< name >lee </ name >
< age >30 </ age >
</ person >
< person id ="20" >
< name >leo </ name >
< age >24 </ age >
</ person >
< persons >
< person id ="23" >
< name >lee </ name >
< age >30 </ age >
</ person >
< person id ="20" >
< name >leo </ name >
< age >24 </ age >
</ person >
</persons>
使用DOM方式解析的java代碼:
package xml;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;
public class DOMForXml {
public static void main(String[] args) {
// 使用DocumentBuilderFactory工廠創建DocumentBuilder對象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// 生成DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// 調用完這句后XML文檔解析完成,暫存在內存中
Document document = builder.parse("test.xml");
// 獲得根元素
Element root = document.getDocumentElement();
// 匹配結點,返回結點集
NodeList personNodes = root.getElementsByTagName("person");
for( int i=0;i<personNodes.getLength();i++){
// 遍歷結點集
Element personElement = (Element)personNodes.item(i);
// 獲得結點上的屬性
String id = personElement.getAttribute("id");
// 輸出
System.out.println("id:"+id);
// 獲得該節點下面的子節點
NodeList personChilds = personElement.getChildNodes();
// 遍歷子結點
for( int j=0;j<personChilds.getLength();j++){
// 判斷當前結點是否是元素類型結點
if(personChilds.item(j).getNodeType()==Node.ELEMENT_NODE){
Element childElement = (Element)personChilds.item(j);
// 判斷當前結點
if(childElement.getNodeName().equals("name")){
// 獲得當前結點對應的值
System.out.println("name:"+childElement.getFirstChild().getNodeValue());
} else if(childElement.getNodeName().equals("age")){
System.out.println("age:"+childElement.getFirstChild().getNodeValue());
}
}
}
}
} 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();
}
}
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;
public class DOMForXml {
public static void main(String[] args) {
// 使用DocumentBuilderFactory工廠創建DocumentBuilder對象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// 生成DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// 調用完這句后XML文檔解析完成,暫存在內存中
Document document = builder.parse("test.xml");
// 獲得根元素
Element root = document.getDocumentElement();
// 匹配結點,返回結點集
NodeList personNodes = root.getElementsByTagName("person");
for( int i=0;i<personNodes.getLength();i++){
// 遍歷結點集
Element personElement = (Element)personNodes.item(i);
// 獲得結點上的屬性
String id = personElement.getAttribute("id");
// 輸出
System.out.println("id:"+id);
// 獲得該節點下面的子節點
NodeList personChilds = personElement.getChildNodes();
// 遍歷子結點
for( int j=0;j<personChilds.getLength();j++){
// 判斷當前結點是否是元素類型結點
if(personChilds.item(j).getNodeType()==Node.ELEMENT_NODE){
Element childElement = (Element)personChilds.item(j);
// 判斷當前結點
if(childElement.getNodeName().equals("name")){
// 獲得當前結點對應的值
System.out.println("name:"+childElement.getFirstChild().getNodeValue());
} else if(childElement.getNodeName().equals("age")){
System.out.println("age:"+childElement.getFirstChild().getNodeValue());
}
}
}
}
} 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();
}
}
}