Minidom getElementById not working

2024/10/4 7:22:06

Minidom's getElementById function is returning None for any entry I pass to it.

For example, this code:

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

Prints "None" on my computer.

I must be doing something here wrong but I can't figure it out!

I'm running Python 3.3.2 if that helps.

Answer

I used another approach to get Elemnts by ID (meaning the XML-attribute "id"), since I wanted to only use xml.dom.minidom.

Here is an example from my work:

#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=0for 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 from the example:

    <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>

I found this way more convenient.

https://en.xdnf.cn/q/70636.html

Related Q&A

Optimization on piecewise linear regression

I am trying to create a piecewise linear regression to minimize the MSE(minimum square errors) then using linear regression directly. The method should be using dynamic programming to calculate the dif…

Python: Check if list of named tuples contains particular attribute value

I have a list of named tuples:from collections import namedtupleT = namedtuple(T, [attr1, attr2, attr3, attr4]) t1 = T(T1, 1, 1234, XYZ) t2 = T(T2, 2, 1254, ABC) t3 = T(T2, 2, 1264, DEF) l = [t1, t2, t…

javascript error: arguments[0].scrollIntoView is not a function using selenium on python

Im using Selenium on python and I would like to scroll to an element to click on it. Everywhere I see that the rigth things to do to go directly to the element is to use :driver = webdriver.Chrome() dr…

Uploading a static project to google app engines

Disclaimer: I already asked here, but apparently off-topic. I want to set up a page using this bootstrap template and host it as a static website using the google appengine service. Inside the google_a…

Python cannot import DataFrame

I am trying to use Pandas in Python to import and manipulate some csv file.my code is like:import pandas as pd from pandas import dataframe data_df = pd.read_csv(highfrequency2.csv) print(data_df.col…

Sum of product of combinations in a list

What is the Pythonic way of summing the product of all combinations in a given list, such as:[1, 2, 3, 4] --> (1 * 2) + (1 * 3) + (1 * 4) + (2 * 3) + (2 * 4) + (3 * 4) = 35(For this example I have t…

discord.py: How to get the user who invited/added the bot to his server? [solution]

I want to send a DM to the user, who invited/added the bot to his server. I noticed that its displayed in the audit log. Can I fetch that and get the user or is there a easier way to achieve that? Ex…

How to reorder the keys of a dictionary?

I have multiple dictionaries inside the list. I want to sort the dictionary with the custom key. In my case, I want to sort it using Date key. By that, I mean to move the Date key to the first position…

How do bitwise operations work in Python?

I have been learning about Bitwise operations today and I learned that Not (~) inverses all bits, e.g.:01010 to 10101which means ~10 should be -5 but instead I have seen that it is -11 (per the python …

How to split large wikipedia dump .xml.bz2 files in Python?

I am trying to build a offline wiktionary using the wikimedia dump files (.xml.bz2) using Python. I started with this article as the guide. It involves a number of languages, I wanted to combine all th…