Java 遍历 xml

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DoomTest {
    public static void main(String[] args) throws Exception {
        // (1)生成工厂类的对象
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // (2)生成解析器对象
        DocumentBuilder db = factory.newDocumentBuilder();
        // (3)对xml文档进行解析
        Document doc = db.parse(new File("src/ab.xml"));
        // (4)读取ab.xml文档中到底有几个节点?节点的名称、节点的类型以及节点的值
        Node root = doc.getChildNodes().item(0);
        findElementChildren(doc);
        Node root1 = doc.getElementsByTagName("phone").item(0);
        findChildren(doc);
        findAttibutes(root);
        findFixedChild(root);
    }

    public static void findChildren(Document doc) {
        // 先通过文档对象doc得到根节点对象root
        Node root = doc.getElementsByTagName("phone").item(0);
        // 通过root对象得到根节点的所有儿子节点,返回一个节点的集合
        NodeList list = root.getChildNodes();
        for (int a = 0; a < list.getLength(); a++) {
            Node n = list.item(a);
            System.out.println(n.getNodeName() + "---" + n.getNodeType()
                    + "---" + n.getNodeValue());
        }
    }

    public static void findElementChildren(Document doc) {
        // 先通过文档对象doc得到根节点对象root
        Node root = doc.getElementsByTagName("phone").item(0);
        // 通过root对象得到根节点的所有儿子节点,返回一个节点的集合
        NodeList list = root.getChildNodes();
        System.out.println("根节点下的所有子节点的个数:" + list.getLength());
        int count = 0;
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(n.getNodeName() + "---" + n.getNodeType()
                        + "---" + n.getNodeValue());
                count++;
            }
        }
        System.out.println("元素节点的总数为:" + count);
    }

    public static void findFixedChild(Node node) {
        NodeList list = node.getChildNodes();
        int count = 0;
        for (int i = 0; i < list.getLength(); i++) {
            boolean flag = false;
            Node n1 = list.item(i);
            if (n1.getNodeName().equals("phone")) {
                flag = true;
                count++;
            }
            if (flag && count == 2) {
                NodeList list1 = n1.getChildNodes();
                for (int j = 0; j < list1.getLength(); j++) {
                    Node n = list1.item(j);
                    System.out.println(n.getNodeName() + "---"
                            + n.getNodeType() + "---" + n.getNodeValue());
                }
            }
        }
    }

    public static void findAttibutes(Node node) {
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            for (int i = 0; i < map.getLength(); i++) {
                Node d = map.item(i);
                System.out.println(d.getNodeName() + "---" + d.getNodeType()
                        + "---" + d.getNodeValue());
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值