如何使用Python在复杂的XML文件中打印特定标签?

时间:2017-08-02 16:51:44

标签: xml python-2.7 python-3.x

此文件(arq.xml)是实验的大型XML文件的一部分:

<!DOCTYPE sysstat PUBLIC "DTD v2.19 sysstat //EN"
            "http://pagesperso-orange.fr/sebastien.godard/sysstat-2.19.dtd">
            <sysstat>
                <sysdata-version>2.19</sysdata-version>
                <host nodename="ServerLabDoS">
                    <sysname>Linux</sysname>
                    <release>3.16.0-4-686-pae</release>
                    <machine>i686</machine>
                    <number-of-cpus>1</number-of-cpus>
                    <file-date>2017-04-10</file-date>
                    <file-utc-time>10:39:04</file-utc-time>
                    <statistics>
                        <timestamp date="2017-04-10" time="07:50:12" utc="0" interval="119">
                            <memory per="second" unit="kB">
                                <memfree>1140168</memfree>
                                <memused>131440</memused>
                                <memused-percent>10.34</memused-percent>
                                <buffers>10928</buffers>
                                <cached>51716</cached>
                                <commit>510544</commit>
                                <commit-percent>28.87</commit-percent>
                                <active>56880</active>
                                <inactive>29832</inactive>
                                <dirty>44</dirty>
                            </memory>
                            <network per="second">
                                <net-dev iface="lo" rxpck="0.00" txpck="0.00" rxkB="0.00" txkB="0.00" rxcmp="0.00" txcmp="0.00" rxmcst="0.00" ifutil-percent="0.00"/>
                                <net-dev iface="eth0" rxpck="12.58" txpck="11.50" rxkB="11.95" txkB="0.85" rxcmp="0.00" txcmp="0.00" rxmcst="0.00" ifutil-percent="0.00"/>
                            </network>
                        </timestamp>
                        <timestamp date="2017-04-10" time="07:52:01" utc="0" interval="107">
                            <memory per="second" unit="kB">
                                <memfree>1140444</memfree>
                                <memused>131164</memused>
                                <memused-percent>10.31</memused-percent>
                                <buffers>11288</buffers>
                                <cached>51932</cached>
                                <commit>509260</commit>
                                <commit-percent>28.80</commit-percent>
                                <active>57024</active>
                                <inactive>29840</inactive>
                                <dirty>28</dirty>
                            </memory>
                            <network per="second">
                                <net-dev iface="lo" rxpck="0.00" txpck="0.00" rxkB="0.00" txkB="0.00" rxcmp="0.00" txcmp="0.00" rxmcst="0.00" ifutil-percent="0.00"/>
                                <net-dev iface="eth0" rxpck="13.89" txpck="12.69" rxkB="13.71" txkB="0.93" rxcmp="0.00" txcmp="0.00" rxmcst="0.00" ifutil-percent="0.00"/>
                            </network>
                        </timestamp>
                        <timestamp date="2017-04-10" time="07:54:01" utc="0" interval="119">
                            <memory per="second" unit="kB">
                                <memfree>1139716</memfree>
                                <memused>131892</memused>
                                <memused-percent>10.37</memused-percent>
                                <buffers>11664</buffers>
                                <cached>52192</cached>
                                <commit>509148</commit>
                                <commit-percent>28.79</commit-percent>
                                <active>57384</active>
                                <inactive>29948</inactive>
                                <dirty>76</dirty>
                            </memory>
                            <network per="second">
                                <net-dev iface="lo" rxpck="0.00" txpck="0.00" rxkB="0.00" txkB="0.00" rxcmp="0.00" txcmp="0.00" rxmcst="0.00" ifutil-percent="0.00"/>
                                <net-dev iface="eth0" rxpck="13.35" txpck="12.40" rxkB="13.68" txkB="0.91" rxcmp="0.00" txcmp="0.00" rxmcst="0.00" ifutil-percent="0.00"/>
                            </network>
                        </timestamp>
    </statistics>
            </host>
        </sysstat>

我的目标是在memusedrxpck

之间的时间戳打印date="2017-04-10" time="07:50:12"date="2017-04-10" time="07:52:01"

我尝试使用minidom(它的目的是比完整的DOM更简单,也更小):

from xml.dom import minidom

doc = minidom.parse("arq.xml")

# doc.getElementsByTagName returns NodeList
timestamp = doc.getElementsByTagName("timestamp")[0]
print(timestamp.firstChild.data)

但是没有用。我被卡住了。我只读了一个XML文件。 有人能帮助我吗?如何打印标签?

0 个答案:

没有答案