Get Element value with minidom with Python

前端 未结 9 2024
悲&欢浪女
悲&欢浪女 2020-11-29 18:01

I am creating a GUI frontend for the Eve Online API in Python.

I have successfully pulled the XML data from their server.

I am trying to grab the value from

9条回答
  •  感情败类
    2020-11-29 18:40

    The above answer is correct, namely:

    name[0].firstChild.nodeValue
    

    However for me, like others, my value was further down the tree:

    name[0].firstChild.firstChild.nodeValue
    

    To find this I used the following:

    def scandown( elements, indent ):
        for el in elements:
            print("   " * indent + "nodeName: " + str(el.nodeName) )
            print("   " * indent + "nodeValue: " + str(el.nodeValue) )
            print("   " * indent + "childNodes: " + str(el.childNodes) )
            scandown(el.childNodes, indent + 1)
    
    scandown( doc.getElementsByTagName('text'), 0 )
    

    Running this for my simple SVG file created with Inkscape this gave me:

    nodeName: text
    nodeValue: None
    childNodes: []
       nodeName: tspan
       nodeValue: None
       childNodes: []
          nodeName: #text
          nodeValue: MY STRING
          childNodes: ()
    nodeName: text
    nodeValue: None
    childNodes: []
       nodeName: tspan
       nodeValue: None
       childNodes: []
          nodeName: #text
          nodeValue: MY WORDS
          childNodes: ()
    

    I used xml.dom.minidom, the various fields are explained on this page, MiniDom Python.

提交回复
热议问题