JAVA中利用DOM解析XML文档

本文介绍了一个使用Java DOM解析XML配置文件的例子。通过DocumentBuilderFactory创建DocumentBuilder实例,再由DocumentBuilder加载XML文件到Document对象中。文章展示了如何遍历XML文档以获取节点、属性和文本值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复制代码
package org.sws.utils;

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

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

import org.sws.model.Server;

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

public class ConfigurationUtil {
    private String conf = "configuration.xml";
    //Document是XML在内存中的一个镜像,获取了Document就可以通过对内存的操作来实现对XML的操作。
    private Document doc = null ;
    
    public Server getConfig(){
        //从DocumentBuilderFactory中获取一个DocumentBuilder的实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            //使用DocumentBuilder来产生一个Document实例
            doc = db.parse(new File(conf));
            
            //Element代表XML中的一个标签对,可用于获取属性值
            Element element = doc.getDocumentElement();
            //获取该Element的标签名
            System.out.println("Root Element : "+element.getTagName());
            //通过标签名来获取多个节点
            NodeList nodeList = doc.getElementsByTagName("Service");
            System.out.println("NodeList Length : "+nodeList.getLength());
            
            Node node = nodeList.item(0);
            System.out.println("First Node : "+node.getNodeName());
            //通过Agetttributes()方法来获取一个NamedNodeMap实例,该实例包含标签属性值
            NamedNodeMap attrs = node.getAttributes();
            
            for (int i=0; i<attrs.getLength(); i++){
                Node attr = attrs.item(i);
                System.out.println(attr.getNodeName()+" : "+attr.getNodeValue());
            }
            
            NodeList childNodes = node.getChildNodes();
            
            for (int i=0; i<childNodes.getLength(); i++){
                Node child = childNodes.item(i);
                //当子节点是一个Element时才能获取该元素的标签名和属性值
                if (child instanceof Element)
                {
                    System.out.println(child.getNodeName()
                            +" : "+child.getFirstChild().getNodeValue());
                }
            }
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  null ;
    }

}
复制代码

XML文件:

复制代码
<?xml version='1.0' encoding='utf-8'?>
<Server>
    <Service name="SampleWebServer">
        <Listen>8080</Listen>
           <Protocol>HTTP/1.1</Protocol> 
        <Host>localhost</Host>
        <Root>/html</Root>
    </Service>
    
    <Service name="SampleWebServer1">
        <Listen>8081</Listen>
           <Protocol>HTTP/1.1</Protocol> 
        <Host>localhost</Host>
        <Root>/html</Root>
    </Service>
</Server>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值