in Education by
I have many rows in a database that contains XML and I'm trying to write a Python script that will go through those rows and count how many instances of a particular node attribute show up. For instance, my tree looks like: How can I access the attributes 1 and 2 in the XML using Python? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Well we have various ways to parse XML in Python but the best way to use is minidom() but before using minidom() you need to know how to use DOM(Document Object Model) which is the quickest and pretty straight forward:- minidom():- The xml.dom.minidom is a minimal implementation of the Document Object Model interface, with an API similar to that in other languages. It is intended to be simpler than the full DOM and also significantly smaller. Below is the Python code that parses your XML code into Python:- from xml.dom import minidom xmldoc = minidom.parse('items.xml') itemlist = xmldoc.getElementsByTagName('item') print(len(itemlist)) print(itemlist[0].attributes['name'].value) for s in itemlist: print(s.attributes['name'].value) If you do not want to use minidom() then you can use ElementTree which does not require DOM knowledge. To use ElementTree firstly you will have to build an Element instance root from the XML by parsing a file by the following way: import xml.etree.ElementTree as ET root = ET.parse('thefile.xml').getroot() The above code will parse the root from XML then you will use the below-mentioned code:- for type_tag in root.findall('bar/type'): value = type_tag.get('foobar') print(value)

Related questions

0 votes
    How can someone parse a numeric string like "121.5555" to it's corresponding float value 121.5555? or parse a ... str to an int. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I need to parse RFC 3339 strings like "2008-09-03T20:56:35.450686Z" into Python's datetime type. I have found ... best way to do this? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    This my my JSON: { "maps": [ { "id": "AT", "iscategorical": "0" }, { "id": "AT", "iscategorical": "0 ... the values, How can I do it? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am about to build a piece of a project that will need to construct and post an XML document to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I want an efficient way to append one string to another in Python. var1 = "foo" var2 = "bar" var3 = var1 + ... built-in method to use? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    I am getting an 'access is denied' error when I attempt to delete a folder that is not empty. I used the ... that is not empty? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How to copy a file in python? I already read this but found nothing. Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I want to freeze the dos pop-up, I already tried raw-input as mentioned in my study material of Python 2.6 and ... (3**200) raw_input() Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    In Python, How to remove trailing and leading whitespace from str ? For instance check this: " Hey" --> "Hey" " ... "Harsh has a dog" Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How to check which Python version is interpreting my script? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Is there a way to declare a constant in Python? In Java we can create constant values in this manner: ... declaration in Python? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
...