python错误属性错误:';元素';对象没有属性';子节点';在解析xml时
我对python世界非常陌生。我有一个新的要求,我需要在python中打印节点值和节点名称。但是我遇到了一些错误。 有人能帮我吗 这是我的示例xml:python错误属性错误:';元素';对象没有属性';子节点';在解析xml时,python,xml,Python,Xml,我对python世界非常陌生。我有一个新的要求,我需要在python中打印节点值和节点名称。但是我遇到了一些错误。 有人能帮我吗 这是我的示例xml: <task chapnbr="05" sectnbr="41" subjnbr="05" func="210" seq="8" pgblknbr="2" chg="N" key="4B5014E906" revdate="20051230"> <effect effrg="801899" efftext="ZAP
<task chapnbr="05" sectnbr="41" subjnbr="05" func="210" seq="8" pgblknbr="2" chg="N" key="4B5014E906" revdate="20051230">
<effect effrg="801899" efftext="ZAP ALL" />
<title>WING</title>
<refblock>(<grphcref refid=NAX00000>Figure 208</grphcref>)</refblock>
<tfmatr>
<pretopic>
<title>General</title>
<list1>
<l1item>
<para>This procedure is a scheduled maintenance task.</para>
</l1item>
</list1>
</pretopic>
</tfmatr>
<topic>
<title>Zonal Inspection</title>
<subtask chapnbr="05" sectnbr="41" subjnbr="05" func="210" seq="008" pgblknbr="2" chg="N" key="B7A276D9D" revdate="20051230">
<effect effrg="8018" efftext="ZAP ALL" />
<list1>
<l1item>
<para>Do the zonal inspection.</para>
</l1item>
</list1>
</subtask>
</topic>
<graphic chapnbr="05" sectnbr="41" subjnbr="05" func="990" seq="808" pgblknbr="2" chg="N" key=NAX00000 revdate="20051230">
<effect effrg="801899" efftext="ZAP ALL" />
<title>Figure 208. Leading Edge to Front Spar (Outboard of Nacelle Strut) Left Wing - General Visual (External)</title>
<sheet gnbr="811DE0C" sheetnbr="1" chg="N" key="5C152" revdate="20051230">
<effect effrg="801899" efftext="ZAP ALL" />
</sheet>
</graphic>
</task>
我犯了错误
由于xml.etree.ElementTree.Element对象没有属性childNodes,因此您将获得AttributeError
要迭代childern元素,只需对元素中的child执行
def print_node(root):
for node in root:
if node.nodeType == node.ELEMENT_NODE:
print node.tagName,"has value:", node.nodeValue, "and is child of:", node.parentNode.tagName
print_node(node)
def print_node(root):
for node in root:
if node.nodeType == node.ELEMENT_NODE:
print node.tagName,"has value:", node.nodeValue, "and is child of:", node.parentNode.tagName
print_node(node)