So you need to understand the structure of the XML and then use the actual tags you're looking for instead of 'Data'

    item = element.find('Item') 
    print(item.tag ,":",item.text)
    value = element.find('Value') 
    print(value.tag ,":",value.text)

Your actual problem is that you need to change the import you use.

import xml.etree.ElementTree as ET

https://docs.python.org/2/library/xml.etree.elementtree.html

Edit: with the way that's structured, you can get a list of Data elements by saying

for data in root.findall('Data'):
    item = data.find('Item') 
    print(item.tag ,":",item.text)
    value = data.find('Value') 
    print(value.tag ,":",value.text)

Now, understand that if that "Data" tag is not at the root level, then you need to root.find() until you can get to it. In other words, if those "Data" tags are enclosed in some parent tags, you need to root.find("Parent Tag"), hope you get the gist of it

Edit2: Looked at my own msinfo.nfo file and this worked:

disks = root.find(".//Category[@name='Disks']")

for disk in disks:
    item = disk.find('Item')
    print(item.tag ,":",item.text)
    value = disk.find('Value')
    print(value.tag ,":",value.text)

Note: This uses XPath syntax to find the element, which is only available in ElementTree1.3 (Python 2.7 and higher). You can also brute force it by following the structure of the XML and traversing through the tree until you get to Disks. The path was System Summary->Components->Storage->Disks and under Disks were those Data elements with Item and Value as children.

Answer from drez90 on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ xml.etree.elementtree.html
xml.etree.ElementTree โ€” The ElementTree XML API
January 29, 2026 - If not given, the standard XMLParser parser is used. parser must be a subclass of XMLParser and can only use the default TreeBuilder as a target. Returns an iterator providing (event, elem) pairs; it has a root attribute that references the root element of the resulting XML tree once source is fully read. The iterator has the close() method that closes the internal file object if source is a filename.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python - GeeksforGeeks
August 10, 2024 - For the purpose of reading and writing the xml file we would be using a Python library named BeautifulSoup. In order to install the library, type the following command into the terminal. ... Beautiful Soup supports the HTML parser included in Pythonโ€™s standard library, but it also supports a number of third-party Python parsers. One is the lxml parser (used for parsing XML/HTML documents). lxml could be installed by running the following command in the command processor of your Operating system:
Discussions

Python Parse XML file for certain lines and output the line to Text widget - Stack Overflow
I need to search a windows msinfo file (.nfo) for certain lines and print them to a Text widget. I can print(line) ever line in the file and I can output every line to the Text widget but as soon a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 6, 2016
python - Reading line by line the data from an XML file - Stack Overflow
I am trying to find a link which contains http or // or \ and surround with a href tag once its found but when reading line by line from the data read from xml..I see the output is split with each ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
November 21, 2012
Reading only lines that are needed and not the ones that are not needed
I have an xml file that is 1000 lines long, I need to replace all Mathew with Kasper in that xml file. I am able to do that successfully if I read all lines of the xml file(test1.xml) and write the contents to another file(test2.xml) but If I dont want to read all the lines rather read only ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
0
July 21, 2023
Python - Parse Single Line from XML - Stack Overflow
Hopefully this is a quick answer for those experienced. I have a single XML file that contains a URL in it, I'd like to take the URL from the XML and then input it into a downloader script I've wri... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 6, 2017
Top answer
1 of 2
1

So you need to understand the structure of the XML and then use the actual tags you're looking for instead of 'Data'

    item = element.find('Item') 
    print(item.tag ,":",item.text)
    value = element.find('Value') 
    print(value.tag ,":",value.text)

Your actual problem is that you need to change the import you use.

import xml.etree.ElementTree as ET

https://docs.python.org/2/library/xml.etree.elementtree.html

Edit: with the way that's structured, you can get a list of Data elements by saying

for data in root.findall('Data'):
    item = data.find('Item') 
    print(item.tag ,":",item.text)
    value = data.find('Value') 
    print(value.tag ,":",value.text)

Now, understand that if that "Data" tag is not at the root level, then you need to root.find() until you can get to it. In other words, if those "Data" tags are enclosed in some parent tags, you need to root.find("Parent Tag"), hope you get the gist of it

Edit2: Looked at my own msinfo.nfo file and this worked:

disks = root.find(".//Category[@name='Disks']")

for disk in disks:
    item = disk.find('Item')
    print(item.tag ,":",item.text)
    value = disk.find('Value')
    print(value.tag ,":",value.text)

Note: This uses XPath syntax to find the element, which is only available in ElementTree1.3 (Python 2.7 and higher). You can also brute force it by following the structure of the XML and traversing through the tree until you get to Disks. The path was System Summary->Components->Storage->Disks and under Disks were those Data elements with Item and Value as children.

2 of 2
0

Here is my code with your sample data, I know it could be written better but I think this solves your problem :)
you have to find the root(xml) and then iterate it's texts ! you can also use other methods like iterfind for better solutions

xml_file  = "<xml><Item><![CDATA[Model]]></Item><Value><![CDATA[TOSHIB  MK1652GSX SCSI Disk Device]]></Value></xml>"
from xml.etree import ElementTree
root = ElementTree.fromstring(xml_file)

start = root.itertext()

while True:
    try:
        print start.next()
    except StopIteration:
        break

Here is the output:

>>>Model
>>>TOSHIB  MK1652GSX SCSI Disk Device
๐ŸŒ
The Hitchhiker's Guide to Python
docs.python-guide.org โ€บ scenarios โ€บ xml
XML parsing โ€” The Hitchhiker's Guide to Python
from xmlschema import XMLSchema, etree_tostring # load a XSD schema file schema = XMLSchema("your_schema.xsd") # validate against the schema schema.validate("your_file.xml") # or schema.is_valid("your_file.xml") # decode a file data = schmema.decode("your_file.xml") # encode to string s = etree_tostring(schema.encode(data))
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-3610.html
Parse XML line by line
Hi, I want to retrieve the information from XML line that looks like this: 180000 The value I need t is 180000. This line has its unique identifier, which is: 'pdce:ExploratoryDrilling contextRef='FD2013Q4YTD' (the use of only 'pdce:ExploratoryD...
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ read-xml-into-python
How to Read XML Files into Python | LearnPython.com
Thanks to the xmltodict module, ... a few lines. If you just need to extract a few bits of information โ€“ or if you need to perform a custom data processing pipeline when reading the XML file โ€“ you might be better off sticking to the xml or BeautifulSoup modules. But if all you need is a quick way to read the data in the XML file, the xmltodict module does the job just fine. In this article, we went over how to read XML files into Python. We started by understanding ...
Find elsewhere
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python xml file โ€“ how to read, write & parse
Python XML File โ€“ How to Read, Write & Parse
August 12, 2024 - Learn how to create,read, and parse XML Files in Python using minidom class and ElementTree. Python XML Tutorial with Example.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ reading-and-writing-xml-files-in-python
Reading and Writing XML Files in Python
November 30, 2017 - The ElementTree module provides a more "Pythonic" interface to handling XMl and is a good option for those not familiar with the DOM. It is also likely a better candidate to be used by more novice programmers due to its simple interface, which you'll see throughout this article. In this article, the ElementTree module will be used in all examples, whereas minidom will also be demonstrated, but only for counting and reading XML documents.
๐ŸŒ
Real Python
realpython.com โ€บ python-xml-parser
A Roadmap to XML Parsers in Python โ€“ Real Python
September 25, 2023 - If youโ€™re looking for a one-liner that could turn your XML document into a Python object, then look no further. While it hasnโ€™t been updated in a few years, the untangle library might soon become your favorite way of parsing XML in Python. Thereโ€™s only one function to remember, and it accepts a URL, a filename, a file object, or an XML string:
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ python-xml-parser-tutorial
Python XML Parser Tutorial | ElementTree and Minidom Parsing | Edureka
December 5, 2024 - In this Python XML Parser Tutorial, you will learn how to parse, read, modify and find elements from XML files in Python using ElementTree and Minidom.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.read_xml.html
pandas.read_xml โ€” pandas 3.0.1 documentation - PyData |
Read XML document into a DataFrame object. ... String path, path object (implementing os.PathLike[str]), or file-like object implementing a read() function. The string can be a path. The string can further be a URL.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ reading-and-writing-xml-files-in-python-with-pandas
Reading and Writing XML Files in Python with Pandas
August 21, 2024 - The xml.etree.ElementTree module comes built-in with Python. It provides functionality for parsing and creating XML documents. ElementTree represents the XML document as a tree. We can move across the document using nodes which are elements and sub-elements of the XML file. In this approach, we read the file content in a variable and use ET.XML() to parse the XML document from the string constant.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Reading only lines that are needed and not the ones that are not needed - Python Help - Discussions on Python.org
July 21, 2023 - I have an xml file that is 1000 lines long, I need to replace all Mathew with Kasper in that xml file. I am able to do that successfully if I read all lines of the xml file(test1.xml) and write the contents to another fiโ€ฆ
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-xml-elementtree
Python XML Tutorial: Element Tree Parse & Read | DataCamp
December 10, 2024 - Parse and read XML data with Element Tree Python package. Learn how to use xml.etree.elementtree and explore your data through XML today!
Top answer
1 of 2
9
>>> code = '''<program new-version="1.1.1.1" name="ProgramName">
... <download-url value="http://website.com/file.exe"/>
... </program>'''

With lxml:

>>> import lxml.etree
>>> lxml.etree.fromstring(code).xpath('//download-url/@value')[0]
'http://website.com/file.exe'

With the built-in xml.etree.ElementTree:

>>> import xml.etree.ElementTree
>>> doc = xml.etree.ElementTree.fromstring(code)
>>> doc.find('.//download-url').attrib['value']
'http://website.com/file.exe'

With the built-in xml.dom.minidom:

>>> import xml.dom.minidom
>>> doc = xml.dom.minidom.parseString(code)
>>> doc.getElementsByTagName('download-url')[0].getAttribute('value')
u'http://website.com/file.exe'

Which one you pick is entirely up to you. lxml needs to be installed, but is the fastest and most feature-rich library. xml.etree.ElementTree has a funky interface, and its XPath support is limited (depends on the version of the python standard library). xml.dom.minidom does not support xpath and tends to be slower, but implements the cross-plattform DOM.

2 of 2
1
 import lxml
 from lxml import etree
 et = etree.parse("your xml file or url")
 value = et.xpath('//download-url/@value')
 print "".join(value)

output = 'http://website.com/file.exe'

you can also use cssselect

 f = open("your xml file",'r')
 values = f.readlines()
 values = "".join(values)
 import lxml.html
 doc = lxml.html.fromstring(values)
 elements = doc.cssselect('document program download-url') //csspath using firebug
 elements[0].get('value')

output = 'http://website.com/file.exe'

๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-read-xml-file-in-python
How to read XML file in Python - Studytonight
In this article, we will learn how to use different parsing modules to read XML documents in Python and some related custom examples as well.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ xml-parsing-python
XML parsing in Python - GeeksforGeeks
June 28, 2022 - for item in root.findall('./channel/item'): Now, once you have taken a look at the structure of your XML file, you will notice that we are interested only in item element. ./channel/item is actually XPath syntax (XPath is a language for addressing parts of an XML document). Here, we want to find all item grand-children of channel children of the root(denoted by '.') element. You can read more about supported XPath syntax here.
๐ŸŒ
lxml
lxml.de โ€บ parsing.html
Parsing XML and HTML with lxml
Otherwise, you can resort at runtime ... slower) Python tools for passing decompressed input into lxml or reading from the network. If you want to parse from a string (bytes or text) and still provide a base URL for the document (e.g. to support relative paths in an XInclude), you can pass the base_url keyword argument: >>> root = etree.fromstring(xml, ...