dom解析xml文件
来源:互联网 发布:windows怎么系统还原 编辑:程序博客网 时间:2024/05/05 13:46
package com.yangrs;
import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
*
* @author spirit_fly dom解析xml
*
*/
public class DomTest {
public static void main(String[] args) {
try {
javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document dom = db.parse("myxml1.xml");
System.out.println("文件路径:" + dom.getDocumentURI());
org.w3c.dom.Element root = dom.getDocumentElement();
Iterator(root); //循环迭代
// for(int i = 0; i < nodelist.getLength(); i++) {
// System.out.println(nodelist.item(i).getNodeName());
// System.out.println(nodelist.item(i).getNodeValue());
// System.out.println(nodelist.item(i).getTextContent());//这里才取得了每个结点的值
//
// //会有七个节点,because空格也算
// NodeList childlist = nodelist.item(i).getChildNodes();
//
// for(int j = 0; j < childlist.getLength();j++) {
// System.out.println(childlist.item(j).getNodeName());
// }
//
// }
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 做循环使用
*
* @param root
* 传入的元素
*/
public static void Iterator(Element root) {
NodeList nodelist = root.getChildNodes();
for (int i = 0; i < nodelist.getLength(); i++) {
Node node = nodelist.item(i);
if (node instanceof Text) {
String value = node.getNodeValue();
if (value != null && !value.trim().equals("")) {
System.out.println("文本:" + value);
}
}
if (node instanceof Element) {
System.out.println("节点:" + node.getNodeName());
Iterator((Element) node);
}
}
}
}
import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
*
* @author spirit_fly dom解析xml
*
*/
public class DomTest {
public static void main(String[] args) {
try {
javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document dom = db.parse("myxml1.xml");
System.out.println("文件路径:" + dom.getDocumentURI());
org.w3c.dom.Element root = dom.getDocumentElement();
Iterator(root); //循环迭代
// for(int i = 0; i < nodelist.getLength(); i++) {
// System.out.println(nodelist.item(i).getNodeName());
// System.out.println(nodelist.item(i).getNodeValue());
// System.out.println(nodelist.item(i).getTextContent());//这里才取得了每个结点的值
//
// //会有七个节点,because空格也算
// NodeList childlist = nodelist.item(i).getChildNodes();
//
// for(int j = 0; j < childlist.getLength();j++) {
// System.out.println(childlist.item(j).getNodeName());
// }
//
// }
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 做循环使用
*
* @param root
* 传入的元素
*/
public static void Iterator(Element root) {
NodeList nodelist = root.getChildNodes();
for (int i = 0; i < nodelist.getLength(); i++) {
Node node = nodelist.item(i);
if (node instanceof Text) {
String value = node.getNodeValue();
if (value != null && !value.trim().equals("")) {
System.out.println("文本:" + value);
}
}
if (node instanceof Element) {
System.out.println("节点:" + node.getNodeName());
Iterator((Element) node);
}
}
}
}
- DOM解析XML文件
- xml文件解析-DOM
- dom解析xml文件
- DOM解析XML文件
- dom解析xml文件
- Dom解析XML文件
- DOM解析XML文件
- Dom 解析XML文件
- DOM解析XML文件
- DOM解析XML文件
- DOM解析XML文件
- DOM 解析XML文件
- DOM解析xml文件
- DOM解析xml文件
- DOM解析XML文件
- Dom解析xml文件
- DOM解析XMl文件
- dom解析XML文件
- 数据库范式
- 逆境成就人生
- 十四种情绪可以左右成功
- 什么是嵌入式编程?什么是ARM?如何学习?
- CSS中背景background-position负值定位深入理解
- dom解析xml文件
- 什么是嵌入式编程?什么是ARM?如何学习? (续)
- sax解析xml文件
- ARM与嵌入式linux的入门建议
- Telerik RadControls for Silverlight Q3 2008可用,要是有20081117就更好了,有DATAGRID用
- 嵌入式系统设计师考试笔记之嵌入式系统基础知识
- 雪崩效应造成处理阻塞
- MYSQL模拟真实环境下的性能
- 跳蚤和绵羊