• xml解析/读取--dom4j


    过程:

    1.new一个操作文件的对象

    2.获取根节点的对象,通过对象获取根节点下子节点的数据

    如有子子节点,过程一样

    代码如下:

            SAXReader reader = new SAXReader();    
            // 通过SAXReader对象的read方法加载xy.xml文件,获取docuemnt对象。
            Document document = reader.read(new File("src/xy.xml"));
            Element xy = document.getRootElement();    //通过获取根节点xy
            Iterator it1 = xy.elementIterator();    //获取根节点的迭代器it1
            while (it1.hasNext()) {        //遍历根下的子节点
                Element side = (Element) it1.next();    //获取根节点的迭代器it1中的元素,即:子节点中的元素
                Iterator it2 = side.elementIterator();    //获取子节点side的迭代器
                while (it2.hasNext()) {        //遍历子节点的子节点
                    Element sideChild = (Element) it2.next();    //获取子节点迭代器it2中的元素,即:子子节点中的元素
                    Iterator it3 = sideChild.elementIterator();
                    while (it3.hasNext()) {
                        Element right = (Element) it3.next();
                        Iterator it4 = right.elementIterator();
                        if(right.getName() == "x"){
                            double dx = Double.valueOf(right.getStringValue());//获取x的值
                            System.out.println(dx);
                        }
                    }
                }
            }

    xml文件如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <xy>
        <constraint id="6">
            <width>150</width>
            <heigth>80</heigth>
            <screenwidth>1080</screenwidth>
        </constraint>
        <side id="1">
            <name>冰与火之歌</name>
            <author>乔治马丁</author>
            <year>2014</year>
            <price>89</price>
            <right>
                <x>12.12</x>
                <y>13.13</y>
            </right>
            <left>
                <x>132.12</x>
                <y>153.13</y>
            </left>
        </side>
    </xy>
  • 相关阅读:
    codeforce 266c Below the Diagonal 矩阵变换 (思维题)
    8月21日训练日记
    CodeForces 651B Beautiful Paintings
    CodeForces 651 C Watchmen
    CodeForces 17D Notepad(同余定理)
    CodeForces 19B Checkout Assistant
    Code Forces 18D Seller Bob(简单DP)
    HOJ Recoup Traveling Expenses(最长递减子序列变形)
    ZOJ 3469Food Delivery(区间DP)
    Code Forces 149DColoring Brackets(区间DP)
  • 原文地址:https://www.cnblogs.com/jiaoqiang/p/8274614.html
Copyright © 2020-2023  润新知