用Document解析XML实例

实例:以下用了三种方式获取XML中的标签值
package com.meixin.xml;

import java.io.File;
import java.io.IOException;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationExcep tion;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLParse
{
    Document document = null;

    NodeList allNode = null;

    //构造函数,初始化Document对象
    public XMLParse()
    {
        //指定File文件
        Filefile = new File( "MyXml.xml");

        //建立DocumentBuilderFactory对象
        DocumentBuilderFactorybuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilderbuilder;
        try
        {
            //建立DocumentBuilder对象
            builder= builderFactory.newDocumentBuilder();
            //用DocumentBuilder对象的parse方法引入文件建立Document对象
            document= builder.parse(file);
            allNode= document.getChildNodes();
               
            //测试1:找出所有person标签,返回NodeList
            NodeListperson = document.getElementsByTagName_r( "person");
            //按条件输出peron标签中的属性和值
            showByCondition(person);

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

            //测试3:按标签名查找输出
            this.SearchByCondition(person);
        }
        catch(ParserConfigurationExcep tion e)
        {
            e.printStackTrace();
        }
        catch(SAXException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            System.err.println( "找不到你指定的文件!");
            e.printStackTrace();
        }
    }

    //例:寻找遍历
    public voidsearchAndShow(NodeList allNode)
    {
        System.out.println();
        System.out.println( "------searchAndShow输出结果-------");
        for( int i = 0; i <allNode.getLength(); i++)
        {
            //得到一个节点,需要强制转换为Element,这里Node是Element的父类
            Nodenode = 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 voidshowByCondition(NodeList allNode)
    {
        System.out.println();
        System.out.println( "------showByCondition输出结果-------");
        Elementelement;
        //对符合条件的所有节点进行遍历
        for( int i = 0; i <allNode.getLength(); i++)
        {
            //获得一个元素
            element= (Element) allNode.item(i);
            //输出这个元素的personid属性
            System.out.println(element.getAttribute( "personid"));
            //此元素有子节点,获取所有子节点,返回一个personList
            NodeListpersonList = element.getChildNodes();
            //遍历所有子节点
            for( int j = 0; j <personList.getLength(); j++)
            {
                //若子节点的名称不为#text,则输出,#text为反/标签
                if(!personList.item(j).getNodeName().equals( "#text"))
                {
                    //输出节点名称、节点值
                    System.out.println(personList.item(j).getNodeName()+ ":"
                            +personList.item(j).getTextContent());
                }
            }
        }
    }

    //按标签名寻找节点
    public voidSearchByCondition(NodeList allNode)
    {
        System.out.println();
        System.out.println( "------SearchByCondition输出结果-------");
        Elementelement;
        //对符合条件的所有节点进行遍历
        for( int i = 0; i <allNode.getLength(); i++)
        {
            //用document来查找所有name标签,得到一个标签后输出值
            Stringname = document.getElementsByTagName_r( "name").item(i)
                    .getTextContent();
            System.out.println( "姓名:"+ name);

            //用document来查找所有tel标签,得到一个标签后输出值
            Stringtel = document.getElementsByTagName_r( "tel").item(i)
                    .getTextContent();
            System.out.println( "电话:"+ tel);

            用document来查找所有tel标签,得到一个标签后输出值
            Stringsex = document.getElementsByTagName_r( "email").item(i)
                    .getTextContent();
            System.out.println( "email:"+ sex);
            System.out.println( "===================");
        }
    }

    public static void main(String[] args)
    {
        newXMLParse();
    }
}
输出结果:
------showByCondition输出结果-------
e01
name:张三
tel:5128
email:txq512@sina.com
e02
name:meixin
tel:5252525
email:wnight88@sina.com
e03
name:yu
tel:5389654
email:yu@188.net

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

------SearchByCondition输出结果-------
姓名:张三
电话:5128
email:txq512@sina.com
===================
姓名:meixin
电话:5252525
email:wnight88@sina.com
===================
姓名:yu
电话:5389654
email:yu@188.net
===================
MyXml.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 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值