用JAVA读取XML文件

来源:互联网 发布:迪杰斯特拉算法例题 编辑:程序博客网 时间:2024/05/20 11:52

解析XML的步骤如下:

1.创建DocumentBuilder工厂
2.创建DocumentBuilder对象
3.DocumentBuilder对象的parse方法得到Document对象
4.Document对象的getElementsByTagName得到NodeList集合
5.通过getFirstChild和getNextSibling进行遍历


用到的包:

import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;


用到的对象:

DocumentBuilderFactory:创建DocumentBuilder的抽象工厂

DocumentBuilder:可以从 XML 获取一个 Document

Document:提供供对文档数据的基本访问


用到的方法:

DocumentBuilder.parse(String)’:将给定 URI 的内容解析为一个 XML 文档,并且返回一个新的 DOM Document对象

Document.getElementsByTagName(String)’:返回具有给定标记名称的所有 Element 的 NodeList

Element.getAttribute(String)’:通过名称获得属性值


下面来解析一个XML文件

import javax.xml.parsers.*;import org.w3c.dom.*;import org.xml.sax.*;public class Test{    public static void main(String[] args)    {        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();        try        {            DocumentBuilder db = dbf.newDocumentBuilder();            Document doc = db.parse("pet2.xml");            NodeList dogList = doc.getElementsByTagName("dog");            System.out.println("共有" + dogList.getLength() + "个dog节点");            for (int i = 0; i < dogList.getLength(); i++)            {                Node dog = dogList.item(i);                Element elem = (Element) dog;                System.out.println("id:" + elem.getAttribute("id"));                for (Node node = dog.getFirstChild(); node != null; node = node.getNextSibling())                {                    if (node.getNodeType() == Node.ELEMENT_NODE)                    {                        String name = node.getNodeName();                        String value = node.getFirstChild().getNodeValue();                        System.out.print(name + ":" + value + "\t");                    }                }                System.out.println();            }        }        catch (Exception e)        {            e.printStackTrace();        }    }}

XML文件

<pets>    <dogs>        <dog id="1">                        <name>YAYA</name>            <health>100</health>            <love>0</love>            <strain>酷酷的雪娜瑞</strain>        </dog>        <dog id="2">                        <name>OUOU</name>            <health>90</health>            <love>15</love>            <strain>聪明的拉布拉多犬</strain>        </dog>    </dogs>    <penguins>        <penguin id="3">                        <name>QQ</name>            <health>100</health>            <love>20</love>            <sex>Q仔</sex>                   </penguin>          </penguins></pets>
0 0
原创粉丝点击