码迷,mamicode.com
首页 > 编程语言 > 详细

Java中Dom解析xml的一个实例

时间:2016-05-12 15:49:57      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

package com.xd.test;

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 test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();//创建解析器工厂对象
            DocumentBuilder db = dbf.newDocumentBuilder();//创建解析器
            Document doc = db.parse("src/first.xml");//创建xml文档对象

            Element e = (Element) doc.getElementsByTagName("school").item(0);
            NodeList n1 = e.getElementsByTagName("grade");
            for (int i = 0; i < n1.getLength(); i++) {
                Element e1 = (Element) n1.item(i);
                NodeList n2 = e.getElementsByTagName("class");
                System.out.println("学员姓名\t学员性别\t学员年龄");
                for (int k = 0; i < n2.getLength(); i++) {
                    Element el = (Element) n2.item(i);
                    NodeList stus = el.getElementsByTagName("student");
                    for (int j = 0; j < stus.getLength(); j++) {
                        Element stu = (Element) stus.item(j);
                        String stuName = stu.getAttribute("name");
                        String stuSex = stu.getAttribute("sex");
                        String stuAge = stu.getAttribute("age");
                        System.out.println(stuName + "\t" + stuSex + "\t"
                                + stuAge);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<school>
    <grade name="一年级">
        <class name="一班">
            <student name="三毛" sex="男" age="20" />
            <student name="小强" sex="男" age="20" />
            <student name="小红" sex="女" age="20" />
        </class>
        <class name="二班">
            <student name="三毛" sex="男" age="20" />
            <student name="小强" sex="男" age="20" />
            <student name="小红" sex="女" age="20" />
        </class>
    </grade>
    <grade name="二年级">
        <class name="一班">
            <student name="三毛" sex="男" age="20" />
            <student name="小强" sex="男" age="20" />
            <student name="小红" sex="女" age="20" />
        </class>
        <class name="二班">
            <student name="三毛" sex="男" age="20" />
            <student name="小强" sex="男" age="20" />
            <student name="小红" sex="女" age="20" />
        </class>
    </grade>
    <grade name="三年级">
        <class name="一班">
            <student name="三毛" sex="男" age="20" />
            <student name="小强" sex="男" age="20" />
            <student name="小红" sex="女" age="20" />
        </class>
        <class name="二班">
            <student name="三毛" sex="男" age="20" />
            <student name="小强" sex="男" age="20" />
            <student name="小红" sex="女" age="20" />
        </class>
    </grade>
</school>

Java中Dom解析xml的一个实例

标签:

原文地址:http://blog.csdn.net/qq_27399407/article/details/51364771

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