码迷,mamicode.com
首页 > 其他好文 > 详细

XML(DOM解析)

时间:2016-08-15 14:25:10      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

//创建book.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>微冷的雨</name>
        <price>100.0</price>
    </book>
</books>

//解析代码

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class MyDom {

    public static void main(String[] args) throws Exception {
        readXml();
    }
    private static void readXml() throws Exception {
        DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("src/book.xml");
        NodeList list = document.getElementsByTagName("book");
        for (int i = 0; i < list.getLength(); i++) {
            Element item = (Element)list.item(i);
            System.out.println(item.getElementsByTagName("name").item(0).getFirstChild().getNodeType());
        }
    }
} 

 

XML(DOM解析)

标签:

原文地址:http://www.cnblogs.com/myhome-1/p/5772661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!