python Minidom getElementById不工作

pgky5nke  于 2023-04-19  发布在  Python
关注(0)|答案(4)|浏览(160)

Minidom的getElementById函数对于我传递给它的任何条目都返回None。
例如,下面的代码:

l = minidom.parseString('<node id="node">Node</node>')
print(l.getElementById("node"))

在我的计算机上打印“无”。
我一定是哪里做错了,但我想不出来!
我正在运行Python 3.3.2,如果有帮助的话。

30byixjq

30byixjq1#

我使用另一种方法通过ID(即XML属性“id”)获取元素,因为我只想使用xml.dom.minidom.
以下是我工作中的一个例子:

#import minidom
from xml.dom.minidom import parse as p
#parse your XML-document
cmmn_doc = p("document.xml")
#Get all child nodes of your root-element or any element surrounding your "target" (in my example "cmmn:casePlanModel")
notelist = cmmn_doc.getElementsByTagName("cmmn:casePlanModel")[0].childNodes

#Now find the element via the id-tag
def find_element(id):
    i=0
    for i in range(len(notelist)):
        if notelist[i].getAttribute("id") == id:
        return notelist[i].nodeName #(or whatever you want to do)

#Call find_element with the id you are looking for
find_element(id)

示例中的XML:

<cmmn:casePlanModel id="CasePlanModel_1" name="A CasePlanModel">
      <cmmn:planItem id="PlanItem_1" definitionRef="Task_1" />
      <cmmn:planItem id="PlanItem_08uai3q" definitionRef="HumanTask_0pgsk2i" />
      <cmmn:planItem id="PlanItem_0crahv8" definitionRef="HumanTask_0jvecsr">
        <cmmn:itemControl id="PlanItemControl_0tdwp8g">
          <cmmn:repetitionRule id="RepetitionRule_03ky93m" />
          <cmmn:requiredRule id="RequiredRule_1klzaio" />
          <cmmn:manualActivationRule id="ManualActivationRule_1rek2bf" />
        </cmmn:itemControl>
      </cmmn:planItem>
      <cmmn:planItem id="PlanItem_08kswcr" definitionRef="HumanTask_14zxi11" />
      <cmmn:planItem id="PlanItem_12b1nkx" definitionRef="ProcessTask_10xuu3g">
        <cmmn:exitCriterion id="EntryCriterion_09gio4l" sentryRef="Sentry_0hst9b5" />
      </cmmn:planItem>
      <cmmn:planItem id="PlanItem_1v34h5m" definitionRef="CaseTask_0hwjce3">
        <cmmn:entryCriterion id="EntryCriterion_1j8r6j1" sentryRef="Sentry_1ii8w5d" />
      </cmmn:planItem>
      <cmmn:planItem id="PlanItem_0wroqsx" definitionRef="EventListener_17yxe7z" />
      <cmmn:sentry id="Sentry_0hst9b5" />
      <cmmn:sentry id="Sentry_1ii8w5d">
        <cmmn:planItemOnPart id="PlanItemOnPart_1gt5jrc" sourceRef="PlanItem_12b1nkx">        <cmmn:standardEvent>complete</cmmn:standardEvent>
</cmmn:planItemOnPart>
        <cmmn:planItemOnPart id="PlanItemOnPart_01b6uw3" sourceRef="PlanItem_0wroqsx">        <cmmn:standardEvent>occur</cmmn:standardEvent>
</cmmn:planItemOnPart>
      </cmmn:sentry>
      <cmmn:task id="Task_1" name="Simple Task" />
      <cmmn:humanTask id="HumanTask_0pgsk2i" name="Human Task" />
      <cmmn:humanTask id="HumanTask_0jvecsr" name="Human_Blocking" isBlocking="false" />
      <cmmn:humanTask id="HumanTask_14zxi11" name="Human_mit_Anhang">
        <cmmn:planningTable id="PlanningTable_1yxv7gm">
          <cmmn:discretionaryItem id="DiscretionaryItem_0ne79yh" definitionRef="DecisionTask_1ecc5v8" />
        </cmmn:planningTable>
      </cmmn:humanTask>
      <cmmn:decisionTask id="DecisionTask_1ecc5v8" name="Descritionary to Human Task" />
      <cmmn:processTask id="ProcessTask_10xuu3g" name="Prozess Task" />
      <cmmn:caseTask id="CaseTask_0hwjce3" name="Case Task" />
      <cmmn:eventListener id="EventListener_17yxe7z" name="EventListener" />
    </cmmn:casePlanModel>

我觉得这样更方便。

33qvvth1

33qvvth12#

如果要获取name=“node”的元素

l.getElementsByTagName("node")

如果你想获取属性为id值为node的元素,可以使用xpath

import xpath
xpath.find("//*['id=node']",l) #search for all elements with an attribute id="node"
iklwldmw

iklwldmw3#

从你输入的指令中,我知道你试图获取id值为node的元素。
解决方案是循环遍历所有XML元素(在这种情况下只有一个元素,但这并不重要),然后检查该元素是否具有名为id的属性,并且该属性的值为node
让我们把这个逻辑转换成一个程序:

>>> from xml.dom import minidom
>>> xml_string = '<node id="node">Node</node>'
>>> xml_doc = minidom.parseString(xml_string)
>>> elements = xml_doc.getElementsByTagName('node')
>>> for element in elements:
...     if element.hasAttribute('id') and element.getAttribute('id') == 'node':
...             print(element.toxml())
... 
<node id="node">Node</node>
wgmfuz8q

wgmfuz8q4#

def getElementById(doc, _id):
    for c in doc.childNodes:
        if c.nodeType not in [c.DOCUMENT_NODE, c.TEXT_NODE, c.DOCUMENT_TYPE_NODE, c.COMMENT_NODE]:
        #[ATTRIBUTE_NODE, CDATA_SECTION_NODE, ENTITY_NODE, PROCESSING_INSTRUCTION_NODE, NOTATION_NODE
            if c.getAttribute('id') == _id:
                return c
        res_for_c = getElementById(c, _id)
        if res_for_c:
            return res_for_c
    return None

相关问题