gpt4 book ai didi

python - Minidom getElementById 不工作

转载 作者:数据小太阳 更新时间:2023-10-29 02:12:15 29 4
gpt4 key购买 nike

对于我传递给它的任何条目,Minidom 的 getElementById 函数都返回 None。

例如,这段代码:

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

在我的电脑上打印“无”。

我一定是这里做错了什么,但我想不通!

如果有帮助,我正在运行 Python 3.3.2。

最佳答案

我使用另一种方法通过 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>

我发现这种方式更方便。

关于python - Minidom getElementById 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24653950/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com