DocumentBuilder 遞歸解析xml,適合android 以及java
package cn;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("d:\\catelog.xml"));
// 獲取根元素節點
Element root = doc.getDocumentElement();
parseElement(root);
}
static void parseElement(Element element) {
String tagName = element.getNodeName();
System.out.print("<" + tagName);
// element元素的所有屬性構成的NamedNodeMap對象,需要對其進行判斷
NamedNodeMap map = element.getAttributes();
// 如果存在屬性,則打印屬性
if (null != map) {
for (int i = 0; i < map.getLength(); i++) {
// 獲得該元素的每一個屬性
Attr attr = (Attr) map.item(i);
// 屬性名和屬性值
String attrName = attr.getName();
String attrValue = attr.getValue();
// 注意屬性值需要加上引號,所以需要\轉義
System.out.print(" " + attrName + "=\"" + attrValue + "\"");
}
}
// 關閉標籤名
System.out.print(">");
// 至此已經打印出了元素名和其屬性
// 下面開始考慮它的子元素
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
// 獲取每一個child
Node node = children.item(i);
// 獲取節點類型
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
// 如果是元素類型,則遞歸輸出
parseElement((Element) node);
} else if (nodeType == Node.TEXT_NODE) {
// 如果是文本類型,則輸出節點值,及文本內容
System.out.print(node.getNodeValue());
} else if (nodeType == Node.COMMENT_NODE) {
// 如果是註釋,則輸出註釋
System.out.print("<!--");
Comment comment = (Comment) node;
// 註釋內容
String data = comment.getData();
System.out.print(data);
System.out.print("-->");
}
}
// 所有內容處理完之後,輸出,關閉根節點
System.out.print("</" + tagName + ">");
}
}
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("d:\\catelog.xml"));
// 獲取根元素節點
Element root = doc.getDocumentElement();
parseElement(root);
}
static void parseElement(Element element) {
String tagName = element.getNodeName();
System.out.print("<" + tagName);
// element元素的所有屬性構成的NamedNodeMap對象,需要對其進行判斷
NamedNodeMap map = element.getAttributes();
// 如果存在屬性,則打印屬性
if (null != map) {
for (int i = 0; i < map.getLength(); i++) {
// 獲得該元素的每一個屬性
Attr attr = (Attr) map.item(i);
// 屬性名和屬性值
String attrName = attr.getName();
String attrValue = attr.getValue();
// 注意屬性值需要加上引號,所以需要\轉義
System.out.print(" " + attrName + "=\"" + attrValue + "\"");
}
}
// 關閉標籤名
System.out.print(">");
// 至此已經打印出了元素名和其屬性
// 下面開始考慮它的子元素
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
// 獲取每一個child
Node node = children.item(i);
// 獲取節點類型
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE) {
// 如果是元素類型,則遞歸輸出
parseElement((Element) node);
} else if (nodeType == Node.TEXT_NODE) {
// 如果是文本類型,則輸出節點值,及文本內容
System.out.print(node.getNodeValue());
} else if (nodeType == Node.COMMENT_NODE) {
// 如果是註釋,則輸出註釋
System.out.print("<!--");
Comment comment = (Comment) node;
// 註釋內容
String data = comment.getData();
System.out.print(data);
System.out.print("-->");
}
}
// 所有內容處理完之後,輸出,關閉根節點
System.out.print("</" + tagName + ">");
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.