Java_读取xml文件;
来源:互联网 发布:node formidable 编辑:程序博客网 时间:2024/06/05 05:00
功能:java读取xml文件源码;
=>person.xml
<?xml version="1.0" encoding="UTF-8"?><book><person a='av' b='bv'> <first>wang</first> <last>laohu</last> <age>25</age> <version>中国邮电出版社</version> </person> <person> <first>li</first> <last>junjia</last> <age>24</age> <version>清华大学出版社</version> </person></book>
=>JavaReadXml.java
package xmlRead;import org.w3c.dom.*;import javax.xml.parsers.*;import java.io.*;/** * xml文件读取类 * @author 23_11 * @time: 2013-09-01 * @url: http://developer.51cto.com/art/200906/128418.htm */public class JavaReadXml {/* * Document可以看作是XML在内存中的镜像,那么一旦获取这个Document就意味着可以通过对 * 内存的操作来实现对XML的操作。所以第一步获取XML相关的Document对象。 */ private Document doc = null;/** * 初始化 * @param xmlFile * @throws Exception */public void init(String xmlFile) throws Exception {// 文档创建器工厂 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();// 文档创建器DocumentBuilder db = dbf.newDocumentBuilder();// 文档对象 doc = db.parse(new File(xmlFile));// 解析;}/** * 输出xml信息 * @param xmlFile * @throws Exception */ public void viewXML(String xmlFile) throws Exception {this.init(xmlFile);// 获取文档对象;/* * 根元素:在xml文件里,只有一个根元素,先把根元素拿出来看看; */Element element = doc.getDocumentElement();System.out.println("=>根元素:" + element.getTagName());NodeList nodeList = doc.getElementsByTagName("person");System.out.println("=>book父节点链的长度:" + nodeList.getLength());Node fatherNode = nodeList.item(0);System.out.println("=>父节点为:" + fatherNode.getNodeName());// 把父节点的属性拿出来 NamedNodeMap attributes = fatherNode.getAttributes();for (int i = 0; i < attributes.getLength(); i++) {Node attribute = attributes.item(i);System.out.println("=>person的属性名为:" + attribute.getNodeName()+ " 相对应的属性值为:" + attribute.getNodeValue());}NodeList childNodes = fatherNode.getChildNodes();System.out.println("=>person子节点:" + childNodes.getLength());for (int j = 0; j < childNodes.getLength(); j++) {Node childNode = childNodes.item(j);// 如果这个节点属于Element ,再进行取值 if (childNode instanceof Element) {System.out.println("子节点名为:" + childNode.getNodeName()+ "相对应的值为" + childNode.getFirstChild().getNodeValue());}}}/** * 测试 * @param args * @throws Exception */public static void main(String[] args) throws Exception {JavaReadXml parse = new JavaReadXml();// 我的XML文件 parse.viewXML("person.xml");}}
=>输出结果:
=>根元素:book=>book父节点链的长度:2=>父节点为:person=>person的属性名为:a 相对应的属性值为:av=>person的属性名为:b 相对应的属性值为:bv=>person子节点:9子节点名为:first相对应的值为wang子节点名为:last相对应的值为laohu子节点名为:age相对应的值为25子节点名为:version相对应的值为中国邮电出版社
- Java_读取xml文件;
- Java_读取properties文件
- java_之读取属性文件
- java_读取文件并对数据进行排序并写出
- SAX 读取xml文件
- xml文件读取
- 读取xml文件内容
- 读取xml 文件
- JAVA读取XML文件
- 读取远程xml文件
- XML文件的读取
- Xml文件的读取
- JAVA读取XML文件
- 读取XML文件
- flash读取xml文件
- C#读取XML文件
- javascript 读取XML文件
- 读取xml 文件
- 最长回文串模板 (Manacher's算法)o(n)复杂度
- onvif开发之设备发现功能的实现
- Oracle 11g服务详细介绍及哪些服务是必须开启的?
- java.lang.IllegalArgumentException: node to traverse cannot be null!
- 新博客
- Java_读取xml文件;
- java设置窗体关闭时执行某些操作
- 数据结构之(二叉排序树)
- ZOJ 3521 Fairy Wars(扫描线)
- 深入浅出CChart 每日一课——第十课 分裂视图,锤子肖哥之错位人格
- java 线程的生命周期
- 矩阵乘法算法
- 新手java基础总结(一)
- Java虚拟机学习分享