xml文件的解析

文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<people>

    <person personid="e01">
        <name>张三</name>
        <tel>5128</tel>
        <email>txq512@sina.com</email>
    </person>

    <person personid="e02">
        <name>meixin</name>
        <tel>5252525</tel>
        <email>wnight88@sina.com</email>
    </person>

    <person personid="e03">
        <name>yu</name>
        <tel>5389654</tel>
        <email>yu@188.net</email>
    </person>
</people>

解析代码如下:

/**
 * Created by tangsikang on 2016/7/12.
 */

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

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

public class XMLParse
{
    Document document = null;

    NodeList allNode = null;

    // 构造函数,初始化Document对象
    public XMLParse()
    {
        // 指定File文件
        File file = new File("app/src/main/res/books.xml");

        // 建立DocumentBuilderFactory对象
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder;
        try
        {
            // 建立DocumentBuilder对象
            builder = builderFactory.newDocumentBuilder();
            // 用DocumentBuilder对象的parse方法引入文件建立Document对象
            document = builder.parse(file);
            allNode = document.getChildNodes();

            // 测试2:遍历所有节点
            searchAndShow(allNode);


        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            System.err.println("找不到你指定的文件!");
            e.printStackTrace();
        }
    }

    // 例:寻找遍历
    public void searchAndShow(NodeList allNode)
    {
        System.out.println();
        System.out.println("------searchAndShow输出结果-------");
        for (int i = 0; i < allNode.getLength(); i++)
        {
            // 得到一个节点,需要强制转换为Element,这里Node是Element的父类
            Node node = allNode.item(i);
            if (!node.getNodeName().equals("#text"))
            {
                System.out.println("节点名称:" + node.getNodeName());
            }
            // System.out.println(element.getAttribute(""));

            if (node.getChildNodes().getLength() > 3)
            {
                // 若包含子节点,则递归遍历
                System.out.println("此节点包含子元素!");
                searchAndShow(node.getChildNodes());
            }
            else
            {
                // 若不包含子节点,则输出节点中的内容
                if (!node.getTextContent().trim().equals("")
                        && node.getTextContent() != null)
                {
                    System.out.println("节点值:" + node.getTextContent());
                }
            }
        }
    }


    public static void main(String[] args)
    {
        new XMLParse();
    }
}
输出结果如下:
------searchAndShow输出结果-------
节点名称:people
此节点包含子元素!

------searchAndShow输出结果-------
节点名称:person
此节点包含子元素!

------searchAndShow输出结果-------
节点名称:name
节点值:张三
节点名称:tel
节点值:5128
节点名称:email
节点值:txq512@sina.com
节点名称:person
此节点包含子元素!

------searchAndShow输出结果-------
节点名称:name
节点值:meixin
节点名称:tel
节点值:5252525
节点名称:email
节点值:wnight88@sina.com
节点名称:person
此节点包含子元素!

------searchAndShow输出结果-------
节点名称:name
节点值:yu
节点名称:tel
节点值:5389654
节点名称:email
节点值:yu@188.net
posted @ 2016-07-12 21:05  Tesi1a  阅读(268)  评论(0编辑  收藏  举报