Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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时_Python_Xml - Fatal编程技术网

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

我对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 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)