您的位置:首页 > 编程语言 > Java开发

java读取xml(DOM解析)

2016-06-02 00:00 411 查看
摘要: java读取xml

一、实现效果



二、xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>java基础知识</name>
<author>java之父</author>
<year>1990-01-01</year>
</book>
<book id="2">
<name>MySQL基础知识</name>
<author>教授</author>
<year>2012-05-05</year>
<language>English</language>
</book>
</bookstore>

三、实现代码

package com.ytx.test;

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
* java读取xml文件
* 条件:不知book下具体有多少个节点
* @author tangxi
*
*/
public class ReadXml {

public static void main(String[] args) {
//创建DocumentBuilderFactory对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//创建DocumentBuilder对象
DocumentBuilder db = dbf.newDocumentBuilder();
//Document的parse方法读取xml文件
Document document = db.parse("book.xml");
//获取所有book节点集合
NodeList nodeList = document.getElementsByTagName("book");
//遍历book节点集合
for(int i = 0;i< nodeList.getLength();i++){
System.out.println("======第"+(i+1)+"本书,开始读取======");
Node node = nodeList.item(i);
//获取book节点的属性集合
NamedNodeMap nnm = node.getAttributes();
for(int j = 0; j< nnm.getLength(); j++){
Node n = nnm.item(j);
String s = n.getNodeName();
String v = n.getNodeValue();
System.out.println(s +" :"+ v);
NodeList nl = node.getChildNodes();
for(int k = 0;k < nl.getLength(); k++){
Node no = nl.item(k);
//按节点类型输出“ </name>这里类型<author>”和<author></author>节点类型不同
if(no.getNodeType() == no.ELEMENT_NODE){
System.out.println(no.getNodeName() + " :" + no.getTextContent());
}
}
}
System.out.println("======第"+(i+1)+"本书,结束读取======");
System.out.println();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息