Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试遍历XML:can';t获取子节点';s值_Python_Xml Parsing_Lxml_Elementtree - Fatal编程技术网

Python 尝试遍历XML:can';t获取子节点';s值

Python 尝试遍历XML:can';t获取子节点';s值,python,xml-parsing,lxml,elementtree,Python,Xml Parsing,Lxml,Elementtree,我需要处理一个包含许多节点的大型文件,其结构如下 <category name="28931778o.rjpf"> <name>RequestedName</name> <root>0</root> <online>1</online> <description xml:lang="pt-PT">mydescription </description> <category-links

我需要处理一个包含许多节点的大型文件,其结构如下

<category name="28931778o.rjpf">
<name>RequestedName</name>
<root>0</root>
<online>1</online>
<description xml:lang="pt-PT">mydescription </description>
<category-links/>
<template/>
<parent name="PTW-0092"/>
<custom-attributes>
<custom-attribute name="sortkey" dt:dt="string">RequestedValue</custom-attribute>
<custom-attribute name="ShortLink" dt:dt="string" xml:lang="pt-PT">/Requested_Url.html</custom-attribute>
<custom-attribute name="ShortLinkActivate" dt:dt="string" xml:lang="pt-PT">true</custom-attribute>
...
</category>
它很好用

跑步时

for elem in tree.iterfind('{http://www.cc.com/a}category/{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]'):
    print elem.text
它也很好用 当我想检索这三个值时,问题就来了。我试图找到一个“category”节点,并在其中找到2个请求的值

for elem in tree.iterfind('{http://www.cc.com/a}category'):
    requestedName = elem.find('{http://www.cc.com/a}name').text
    print requestedName
    Requestedsortkey = elem.find('./{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]')
    print Requestedsortkey.text
    RequestedUrl = elem.find('./{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="ShortLink"]')
    print RequestedUrl.text
程序因错误消息而崩溃 AttributeError:“非类型”对象没有属性“文本”


谁能帮忙?

你说得对!非常感谢

for elem in tree.iterfind('{http://www.cc.com/a}category'):
    requestedName = elem.find('{http://www.cc.com/a}name').text
   print requestedName
   Requestedsortkey = elem.find('{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]')
    if Requestedsortkey <> None:
       print Requestedsortkey.text
    RequestedUrl = elem.find('{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="ShortLink"]')
    if RequestedUrl <> None : 
       print RequestedUrl.text
树中元素的
。iterfind('{http://www.cc.com/a}类别“):
requestedName=elem.find('{http://www.cc.com/a}名称“)。文本
打印请求的名称
Requestedsortkey=elem.find('{http://www.cc.com/a}自定义属性/{http://www.cc.com/a}自定义属性[@name=“sortkey”]”)
如果请求SortKey无:
打印Requestedsortkey.text
RequestedUrl=elem.find('{http://www.cc.com/a}自定义属性/{http://www.cc.com/a}自定义属性[@name=“ShortLink”]”)
如果请求,则返回无:
打印RequestedUrl.text

在尝试访问
文本
属性之前,必须检查对象是否为
。我不确定,但我看到了一个区别-在新版本中,您在路径中使用
/
,但不使用
名称
。多亏了dopstar
for elem in tree.iterfind('{http://www.cc.com/a}category'):
    requestedName = elem.find('{http://www.cc.com/a}name').text
   print requestedName
   Requestedsortkey = elem.find('{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="sortkey"]')
    if Requestedsortkey <> None:
       print Requestedsortkey.text
    RequestedUrl = elem.find('{http://www.cc.com/a}custom-attributes/{http://www.cc.com/a}custom-attribute[@name="ShortLink"]')
    if RequestedUrl <> None : 
       print RequestedUrl.text