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>
我发现这种方式更方便。
如果你想获取name="node"的元素
l.getElementsByTagName("node")
如果你想获取具有属性“id”和值为“node”的属性的元素,请使用xpath:
import xpath
xpath.find("//*['id=node']",l) #search for all elements with an attribute id="node"
根据您输入的说明,我了解到您正在尝试获取
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>
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