我正在尝试查找package元素并在package元素内进行迭代以获取必填字段。我已经提到了一些答案,但对我而言并没有奏效。有人可以建议/帮助我如何解决该问题吗?
问题:在ElementTree中进行迭代时出现AttributeError
以下是我正在尝试的示例代码
import xml.etree.ElementTree as ET
file_name = "sample-primary.xml"
parser = ET.XMLParser(encoding="utf-8")
tree = ET.parse(file_name, parser=parser)
tree_toString = (ET.tostring(tree.getroot()))
NSMAP = {'md':'http://linux.duke.edu/metadata/common'}
for package in tree.findall('.//md:package',namespaces=NSMAP):
title = package.get('name').text
timeStamp = package.find('time').get('file')
print title,timeStamp
以下是我的XML结构。
<?xml version="1.0" encoding="utf-8"?>
<metadata packages="18263" xmlns:rpm="http://linux.duke.edu/metadata/rpm" xmlns="http://linux.duke.edu/metadata/common">
<package type="rpm">
<name>nscd</name>
<arch>x86_64</arch>
<version epoch="0" ver="2.17" rel="157.el7_3.4"></version>
<checksum pkgid="YES" type="sha1">c1352162291453dda15c4dee94070e769704498e</checksum>
<summary>A Name Service Caching Daemon (nscd).</summary>
<description>Nscd caches name service lookups and can dramatically improve
performance with NIS+, and may help with DNS as well.</description>
<packager>Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla></packager>
<url>http://www.gnu.org/software/glibc/</url>
<time build="1495794801" file="1497890965"></time>
<size installed="183744" archive="184812" package="273752"></size>
<location href="Packages/nscd-2.17-157.el7_3.4.x86_64.rpm"></location>
<format>
<rpm:license>LGPLv2+ and LGPLv2+ with exceptions and GPLv2+</rpm:license>
<rpm:vendor>Red Hat, Inc.</rpm:vendor>
<rpm:group>System Environment/Daemons</rpm:group>
<rpm:buildhost>x86-037.build.eng.bos.redhat.com</rpm:buildhost>
<rpm:sourcerpm>glibc-2.17-157.el7_3.4.src.rpm</rpm:sourcerpm>
<rpm:header-range start="1384" end="204256"></rpm:header-range>
<rpm:provides>
<rpm:entry ver="2.17" epoch="0" flags="EQ" name="config(nscd)" rel="157.el7_3.4"></rpm:entry>
<rpm:entry ver="2.17" epoch="0" flags="EQ" name="nscd" rel="157.el7_3.4"></rpm:entry>
<rpm:entry ver="2.17" epoch="0" flags="EQ" name="nscd(x86-64)" rel="157.el7_3.4"></rpm:entry>
</rpm:provides>
<rpm:requires>
<rpm:entry name="/bin/sh"></rpm:entry>
<rpm:entry pre="1" name="/bin/sh"></rpm:entry>
<rpm:entry pre="1" name="/usr/sbin/useradd"></rpm:entry>
<rpm:entry name="/usr/sbin/userdel"></rpm:entry>
<rpm:entry ver="1.1.3" epoch="0" flags="GE" name="audit-libs"></rpm:entry>
<rpm:entry pre="1" name="coreutils"></rpm:entry>
<rpm:entry ver="2.17" epoch="0" flags="EQ" name="glibc" rel="157.el7_3.4"></rpm:entry>
<rpm:entry name="libaudit.so.1()(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6()(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.10)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.14)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.17)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.2.5)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.3)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.3.2)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.3.4)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.4)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.7)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.8)(64bit)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.9)(64bit)"></rpm:entry>
<rpm:entry name="libcap.so.2()(64bit)"></rpm:entry>
<rpm:entry name="libnsl.so.1()(64bit)"></rpm:entry>
<rpm:entry name="libnsl.so.1(GLIBC_2.2.5)(64bit)"></rpm:entry>
<rpm:entry name="libpthread.so.0()(64bit)"></rpm:entry>
<rpm:entry name="libpthread.so.0(GLIBC_2.2.5)(64bit)"></rpm:entry>
<rpm:entry name="libpthread.so.0(GLIBC_2.3.2)(64bit)"></rpm:entry>
<rpm:entry name="libpthread.so.0(GLIBC_2.3.3)(64bit)"></rpm:entry>
<rpm:entry ver="1.17.10" epoch="0" flags="GE" name="libselinux" rel="1"></rpm:entry>
<rpm:entry name="libselinux.so.1()(64bit)"></rpm:entry>
<rpm:entry name="systemd-units"></rpm:entry>
<rpm:entry pre="1" name="systemd-units"></rpm:entry>
</rpm:requires>
<file>/etc/nscd.conf</file>
<file>/usr/sbin/nscd</file>
<file type="ghost">/etc/sysconfig/nscd</file>
</format>
</package>
<package type="rpm">
<name>gdm</name>
<arch>i686</arch>
<version epoch="1" ver="3.22.3" rel="11.el7"></version>
<checksum pkgid="YES" type="sha1">772831cd757105c683d7e5bfb8e407e71a5d555a</checksum>
<summary>The GNOME Display Manager</summary>
<description>GDM provides the graphical login screen, shown shortly after boot up,
log out, and when user-switching.</description>
<packager>Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla></packager>
<url>http://download.gnome.org/sources/gdm</url>
<time build="1496157819" file="1496431342"></time>
<size installed="2146497" archive="2172044" package="515256"></size>
<location href="Packages/gdm-3.22.3-11.el7.i686.rpm"></location>
<format>
<rpm:license>GPLv2+</rpm:license>
<rpm:vendor>Red Hat, Inc.</rpm:vendor>
<rpm:group>User Interface/X</rpm:group>
<rpm:buildhost>x86-038.build.eng.bos.redhat.com</rpm:buildhost>
<rpm:sourcerpm>gdm-3.22.3-11.el7.src.rpm</rpm:sourcerpm>
<rpm:header-range start="1384" end="39916"></rpm:header-range>
<rpm:provides>
<rpm:entry ver="3.22.3" epoch="1" flags="EQ" name="config(gdm)" rel="11.el7"></rpm:entry>
<rpm:entry ver="3.22.3" epoch="1" flags="EQ" name="gdm" rel="11.el7"></rpm:entry>
<rpm:entry ver="3.22.3" epoch="1" flags="EQ" name="gdm(x86-32)" rel="11.el7"></rpm:entry>
<rpm:entry ver="3.22.3" epoch="1" flags="EQ" name="gdm-libs(x86-32)" rel="11.el7"></rpm:entry>
<rpm:entry ver="3.22.3" epoch="1" flags="EQ" name="gdm-plugin-fingerprint" rel="11.el7"></rpm:entry>
<rpm:entry ver="3.22.3" epoch="1" flags="EQ" name="gdm-plugin-smartcard" rel="11.el7"></rpm:entry>
<rpm:entry name="libgdm.so.1"></rpm:entry>
<rpm:entry ver="gdm" epoch="0" flags="EQ" name="service(graphical-login)"></rpm:entry>
</rpm:provides>
<rpm:requires>
<rpm:entry name="/bin/sh"></rpm:entry>
<rpm:entry pre="1" name="/bin/sh"></rpm:entry>
<rpm:entry name="/sbin/nologin"></rpm:entry>
<rpm:entry pre="1" name="/usr/sbin/useradd"></rpm:entry>
<rpm:entry name="accountsservice"></rpm:entry>
<rpm:entry ver="1.0.6" epoch="0" flags="GE" name="audit-libs"></rpm:entry>
<rpm:entry name="dconf"></rpm:entry>
<rpm:entry name="gnome-keyring-pam"></rpm:entry>
<rpm:entry name="gnome-session"></rpm:entry>
<rpm:entry ver="2.21.92" epoch="0" flags="GE" name="gnome-settings-daemon"></rpm:entry>
<rpm:entry name="gnome-shell"></rpm:entry>
<rpm:entry name="iso-codes"></rpm:entry>
<rpm:entry name="libX11.so.6"></rpm:entry>
<rpm:entry ver="1.0.4" epoch="0" flags="GE" name="libXau" rel="4"></rpm:entry>
<rpm:entry name="libXau.so.6"></rpm:entry>
<rpm:entry name="libXdmcp.so.6"></rpm:entry>
<rpm:entry name="libaccountsservice.so.0"></rpm:entry>
<rpm:entry name="libaudit.so.1"></rpm:entry>
<rpm:entry name="libc.so.6"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.0)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.1)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.1.2)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.1.3)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.2)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.3)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.3.4)"></rpm:entry>
<rpm:entry name="libc.so.6(GLIBC_2.4)"></rpm:entry>
<rpm:entry name="libcanberra-gtk3.so.0"></rpm:entry>
<rpm:entry name="libcanberra.so.0"></rpm:entry>
<rpm:entry name="libgcc_s.so.1"></rpm:entry>
<rpm:entry name="libgcc_s.so.1(GCC_3.0)"></rpm:entry>
<rpm:entry name="libgcc_s.so.1(GCC_3.3.1)"></rpm:entry>
<rpm:entry name="libgdk-3.so.0"></rpm:entry>
<rpm:entry name="libgdk_pixbuf-2.0.so.0"></rpm:entry>
<rpm:entry name="libgio-2.0.so.0"></rpm:entry>
<rpm:entry name="libglib-2.0.so.0"></rpm:entry>
<rpm:entry name="libgobject-2.0.so.0"></rpm:entry>
<rpm:entry name="libgtk-3.so.0"></rpm:entry>
<rpm:entry name="libkeyutils.so.1"></rpm:entry>
<rpm:entry name="libkeyutils.so.1(KEYUTILS_0.3)"></rpm:entry>
<rpm:entry name="libkeyutils.so.1(KEYUTILS_1.5)"></rpm:entry>
<rpm:entry name="libpam.so.0"></rpm:entry>
<rpm:entry name="libpam.so.0(LIBPAM_1.0)"></rpm:entry>
<rpm:entry name="libpthread.so.0"></rpm:entry>
<rpm:entry name="libpthread.so.0(GLIBC_2.0)"></rpm:entry>
<rpm:entry name="libpthread.so.0(GLIBC_2.1)"></rpm:entry>
<rpm:entry name="libselinux.so.1"></rpm:entry>
<rpm:entry name="libsystemd.so.0"></rpm:entry>
<rpm:entry name="libsystemd.so.0(LIBSYSTEMD_209)"></rpm:entry>
<rpm:entry name="libxcb.so.1"></rpm:entry>
<rpm:entry ver="0.99.8.1" epoch="0" flags="GE" name="pam" rel="11"></rpm:entry>
<rpm:entry name="pulseaudio-gdm-hooks"></rpm:entry>
<rpm:entry name="rtld(GNU_HASH)"></rpm:entry>
<rpm:entry name="setxkbmap"></rpm:entry>
<rpm:entry name="system-logos"></rpm:entry>
<rpm:entry name="systemd"></rpm:entry>
<rpm:entry pre="1" name="systemd"></rpm:entry>
<rpm:entry ver="186" epoch="0" flags="GE" name="systemd"></rpm:entry>
<rpm:entry name="xorg-x11-server-utils"></rpm:entry>
<rpm:entry name="xorg-x11-xinit"></rpm:entry>
</rpm:requires>
<rpm:obsoletes>
<rpm:entry ver="3.12.0" epoch="1" flags="LT" name="gdm-libs" rel="3"></rpm:entry>
<rpm:entry ver="3.2.1" epoch="1" flags="LT" name="gdm-plugin-fingerprint"></rpm:entry>
<rpm:entry ver="3.2.1" epoch="1" flags="LT" name="gdm-plugin-smartcard"></rpm:entry>
</rpm:obsoletes>
<file>/etc/dbus-1/system.d/gdm.conf</file>
<file>/etc/gdm/Init/Default</file>
<file>/etc/gdm/PostLogin/Default.sample</file>
<file>/etc/gdm/PostSession/Default</file>
<file>/etc/gdm/PreSession/Default</file>
<file>/etc/gdm/Xsession</file>
<file>/etc/gdm/custom.conf</file>
<file>/etc/pam.d/gdm-autologin</file>
<file>/etc/pam.d/gdm-fingerprint</file>
<file>/etc/pam.d/gdm-launch-environment</file>
<file>/etc/pam.d/gdm-password</file>
<file>/etc/pam.d/gdm-pin</file>
<file>/etc/pam.d/gdm-smartcard</file>
<file>/usr/bin/gdm-screenshot</file>
<file>/usr/bin/gdmflexiserver</file>
<file>/usr/sbin/gdm</file>
<file type="dir">/etc/dconf/db/gdm.d</file>
<file type="dir">/etc/dconf/db/gdm.d/locks</file>
<file type="dir">/etc/gdm</file>
<file type="dir">/etc/gdm/Init</file>
<file type="dir">/etc/gdm/PostLogin</file>
<file type="dir">/etc/gdm/PostSession</file>
<file type="dir">/etc/gdm/PreSession</file>
</format>
</package>
</metadata>
答案 0 :(得分:1)
ios-select
和name
子元素与time
绑定到相同的名称空间,因此您也需要在package
中指定名称空间。
代码:
find()
输出:
import xml.etree.ElementTree as ET
file_name = "sample-primary.xml"
tree = ET.parse(file_name)
NSMAP = {'md': 'http://linux.duke.edu/metadata/common'}
for package in tree.findall('.//md:package', namespaces=NSMAP):
title = package.find('md:name', namespaces=NSMAP).text
timeStamp = package.find('md:time', namespaces=NSMAP).get('file')
print title, timeStamp