递归打印XML文档树-使用w3cDOM模型解析
2016-03-08 22:38
543 查看
如题,使用w3c xml parse api来把内存中的Document文档树对象打印到控制台
package xml;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
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 TestXml {
public static void main(String[] args) throws Exception {
Class<TestXml> testXmlClazz = TestXml.class;
InputStream is = testXmlClazz.getResourceAsStream("???.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
Element root = doc.getDocumentElement();
parseElement(root);
}
private static void parseElement(Element ele) {
System.out.print("<" + ele.getTagName());
// 打印标签的属性
NamedNodeMap attris = ele.getAttributes();
for(int i = 0; i < attris.getLength(); i++) {
Attr attr = (Attr) attris.item(i);
System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
}
System.out.println(">");
// 获取子标签
NodeList childNodes = ele.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
// 递归处理子标签
parseElement((Element) childNode);
} else if (childNode.getNodeType() != Node.COMMENT_NODE) {
System.out.println(childNode.getTextContent().trim());
}
}
System.out.println("</" + ele.getTagName() + ">");
}
}
package xml;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
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 TestXml {
public static void main(String[] args) throws Exception {
Class<TestXml> testXmlClazz = TestXml.class;
InputStream is = testXmlClazz.getResourceAsStream("???.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
Element root = doc.getDocumentElement();
parseElement(root);
}
private static void parseElement(Element ele) {
System.out.print("<" + ele.getTagName());
// 打印标签的属性
NamedNodeMap attris = ele.getAttributes();
for(int i = 0; i < attris.getLength(); i++) {
Attr attr = (Attr) attris.item(i);
System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\"");
}
System.out.println(">");
// 获取子标签
NodeList childNodes = ele.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
// 递归处理子标签
parseElement((Element) childNode);
} else if (childNode.getNodeType() != Node.COMMENT_NODE) {
System.out.println(childNode.getTextContent().trim());
}
}
System.out.println("</" + ele.getTagName() + ">");
}
}
相关文章推荐
- Python动态类型的学习---引用的理解
- CSS3属性教程与案例分享
- 我是运营,我没有假期
- W3C api 抓取
- XML 与 JSON 优劣对比
- As3.0 xml + Loader应用代码
- 土人系列AS入门教程 -- 对象篇
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- C#托管堆对象实例包含内容分析
- 修复mysql数据库
- C#实现获取不同对象中名称相同属性的方法
- C#针对xml基本操作及保存配置文件应用实例
- Ruby程序中创建和解析XML文件的方法
- javascript asp教程第十一课--Application 对象
- asp下查询xml的实现代码