All Questions
Tagged with xml.etree or elementtree
3,222 questions
0
votes
1
answer
52
views
Python ElementTree returning empty attributes [closed]
The following code:
import xml.etree.ElementTree as ET
essai = '''<?xml version='1.0' standalone='yes' ?>
<LVData xmlns="http://www.ni.com/LVData">
<Version>18.0.1f4</...
0
votes
1
answer
83
views
Generate XML file using ElementTree API
I'm trying to generate the below XML file using the data populated in a list:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="65" failures="0"...
2
votes
1
answer
136
views
How can I read records from an XML file and write them to a CSV?
I am trying to read records from an XML file using a Python script, then write the data to a CSV one record per line. My current implementation is throwing an IndexError when I try to get the first ...
2
votes
2
answers
93
views
Python lxml xpath not working on some elements
I'm having trouble extracting a specific element text from a soap response. Other elements seems to be working fine.
I have tried the following:
Python 3.13.3 (main, Apr 8 2025, 13:54:08) [Clang 16.0....
0
votes
1
answer
71
views
Preserve line breaks in XML attributes when parsing with lxml
I'm trying to batch-process a couple of XML files through a python script, with the XML files having line breaks in some of their attributes like so:
<?xml version='1.0' encoding='UTF-8'?>
<...
0
votes
1
answer
32
views
Python ElementTree iter() method using XPath
I have the following XML element:
<Content>
<Controller Use="Context" Name="Base_Project_Maximum_Connections">
<DataTypes Use="Context">
<...
1
vote
1
answer
63
views
None type when parsing xml data in Python
I'd like to be able to parse some info from xml data while uses namespaces. I have been trying to read/follow steps in:
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-...
0
votes
1
answer
46
views
Using xml.etree.ElementTree to extract values not working
here is the XML:
<multi-routing-engine-results>
<multi-routing-engine-item>
<re-name>node0</re-name>
<source-resource-usage-pool-information>
...
0
votes
2
answers
57
views
trying to pull an value out of xml using xml.etree.ElementTree
I have the following xml:
<multi-routing-engine-results>
<multi-routing-engine-item>
<re-name>node0</re-name>
<source-resource-usage-pool-information>
...
0
votes
1
answer
218
views
What is the best way to control chunk size used by XML iterative parsers in Python, without using SAX? (`iterparse` behaves unexpectedly)
Question: What is the best way to control chunk size used by standard XML iterative parsers in Python?
If single elements* aren't the optimal chunk size for use by iterative parsers, then what is the ...
1
vote
4
answers
146
views
How to replace xml node value in Python, without changing the whole file
Doing my first steps in python I try to parse and update a xml file.
The xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="util/style/...
1
vote
1
answer
56
views
How can I access an XML node's attributes in Python that have a namespace?
I've got a question about parsing a rather complicated XML document in Python with xml.etree.ElementTree. The XML is scap-security-guide-0.1.75/ssg-ubuntu2204-ds.xml from https://github.com/...
-1
votes
3
answers
85
views
Python script to create an XML [duplicate]
I am preparing a Python script to create an XML like:
<?xml version="1.0" encoding="UTF-8"?>
<FileInfo>
<FileFormatVersion>1.0</FileFormatVersion>
&...
1
vote
2
answers
69
views
How to extract data/nodes from .xml stored as string?
I have a .xml in string format xmlString which looks like below
<entry xmlns="http://www.w3.org/2004/tom">
<id>urn:contentItem:7WBG-8H88-Y898-B277-00000-00-1</id>
<...
2
votes
1
answer
97
views
Parsing XML document with namespaces in Python
I am trying to parse xml with namespace and attributes.
I'm using XML library in Python and since I'm new with this, cannot find solution even I checked over this forum, there are similar questions ...
1
vote
1
answer
124
views
Find a particular tag within XML file in Python using xml.etree.ElementTree as ET with method root.find() - does not work as doc describe
I have an XML file "bib_full-001664.xml" and want to find element:
<issn pub-type="ppub">2544-1558</issn>
My XML file:
<OAI-PMH xmlns="http://www.openarchives....
0
votes
2
answers
68
views
How to add namespace when add node to XML with xml.etree.ElementTree?
I have next code to parse one XML string, and then add a new node to the XML. But you can see my code can only add <category term="Platform Version" value="Linux_6.6" /> ...
1
vote
2
answers
81
views
How to merge two xml files at a specific level
I want to merge two xml files using Python:
File1.xml
<?xml version='1.0' encoding='ASCII'?>
<MyData>
<Elements>
<Element>
<ElementID>15</ElementID>
...
0
votes
1
answer
297
views
Convert xml.etree.ElementTree.Element to lxml.Element
I'm developing a package that can be used by both users of lxml and the default xml package. ATM I have a small try:except for the lxml import, but sometimes the user uses functions like dump() from ...
0
votes
1
answer
54
views
Element Tree reads only root element without its contents in certain XML files
I need to make a Python script to fetch cameras configurations and check on the stuff wrote in their OSD. I made code that downloads XML file directly from them and then parses through it in order to ...
0
votes
1
answer
115
views
Using default_namespace argument of ElementTree.tostring throws error
I want to use ElementTree to modify an XML-document and to keep it comparable, I want to have the same namespace-prefixes in the new file as in the old file.
However, the default_namespace= argument ...
0
votes
1
answer
49
views
XML Placement Problem with xml.etree.ElementTree
I'm using Python's xml.etree.ElementTree to generate XML, and I’m encountering an issue where an XML subelement appears under a different tag than intended.
My function looks like this:
def ...
0
votes
1
answer
118
views
Python XML findall() search/parsing issue
Trying to parse an XML report file in my Gitlab runner and the logic is failing on the line below. Simplified the code so I can post for help.
import xml.etree.ElementTree as ET
test_xml = '''
<...
1
vote
1
answer
59
views
How to print only the opening tag of an element in etree?
Given an Element from etree, I would like to print only the opening tag. This would be immensely useful for debugging.
For example:
>>> from lxml import etree
>>> elem = etree....
0
votes
2
answers
115
views
lxml.etree Elements dropping namespace on copy
I am stitching together XML files using the lxml.etree library and namespaces are being dropped on write.
Input.xml
<?xml version="1.0" encoding="UTF-8"?>
<haul>
&...
0
votes
1
answer
59
views
Multi-level listing bullet point "text" not showing in XML tree of Word Document's XML
I am trying to parse Word Documents using their XML; I am doing this through using Python's
xml.etree.ElementTree module. This is the code I used to create a plain .txt file output for a given Word ...
0
votes
1
answer
41
views
Pyrhon parse XML drop XML header
I have the following code to edit XML
import xml.etree.ElementTree as ET
data = b'<?xml version="1.0" encoding="UTF-8"?><aaaa version="1.0"><zzz>xxxxxx&...
0
votes
1
answer
55
views
Parse XML to String with <b> tags [duplicate]
I'm only getting value1 when using .findall('string') and rest is ignored. How to get whole value?
xml file input:
<resources>
<string name="key">value1 <b>value2</b>...
0
votes
1
answer
66
views
change namespace with XML.element.tree
Working with python :
My program opens a template in XML format, insert the user info, and then saves it.
The template is the following :
<ids xmlns="http://standards.buildingsmart.org/IDS&...
0
votes
1
answer
62
views
How can I remove duplicate filenames from different directories in a list and use them once as xml tag text?
I have a file tree like :
2_Product
2-1_CategoryName1_Product
2-1-1_Name1_Product
LLL_nomenclature1_product.zip
LLL_nomenclature1_product (folder)
...
0
votes
1
answer
70
views
ElementTree.find() seems to miss recently appended elements
I can't wrap my head around this one. Why is find() not finding the appended element when it can find the exact same element when it is loaded from an XML string?
The following function creates an ...
0
votes
1
answer
54
views
Parsing .targets file with ElementTree does not find specified tag
I am trying to use ElementTree to parse information out of a .targets file from a NuGet package.
What I am trying to find is the tag <AdditionalIncludeDirectories>. I am using the following ...
0
votes
0
answers
95
views
Force python to write namespace in root tag
I'm using python to generate an XML document for a third party. The third party has very precise ideas about how the final xml file shall look like. In my case it wants me to define a namespace in the ...
1
vote
2
answers
110
views
how to merge 2 xml's in python
XML 1:
<invoiceCopy>
<document>
<invoicenumber>1245678</invoicenumber>
<invoicedate>2024-06-06</invoicedate>
<pdffilename>12345678.pdf</...
-2
votes
1
answer
43
views
Load xml files to a df (to many levels)
I am trying to load data from many xml files with etree.ElementTree.
The data in the xml looks like this sample:
<mydata>
<Record>
<Field name="ParaA">2927695</Field&...
3
votes
0
answers
115
views
What is wrong with using Element.makeelement?
Why does the Python documentation say this about xml.etree.ElementTree's Element.makeelement() method?
Do not call this method, use the SubElement() factory function instead
It's specifically not ...
0
votes
1
answer
56
views
Get value from ElementTree using the full path
Let's say we have the following xml string
xml_string = '''
<Wikimedia>
<projects>
<project name="Wikipedia" launch="2001-01-05">
<editions>
...
0
votes
3
answers
100
views
How would I extract from xml the value of "xml:id" in python using ElementTree into a dataframe
I'm currently in the process of wrangling bibliographic information from an XML structure into, literally, almost anything else useable. My final move is to pull the value of the "xml:id" ...
0
votes
0
answers
27
views
How to parse a what looks like a combined xml file? [duplicate]
I have an app that creates an xml file. It looks like 3 xml files combined into one? I want to parse the file but when opening it with elementtree it shows an error "junk after document element: ...
0
votes
2
answers
57
views
How to write xml file root value with Python?
I have a script which I want to use to create Write Files to update a serial number on a device.
It does a folder/file path check -> Copies the Read.xml and Creates a Write.xml (Copy) -> Obtains ...
0
votes
1
answer
77
views
How to save a xml structure using python tree.write(file_name)? [duplicate]
I wanted to add Element with Subelements to my xml file using Python. But after changing file the Element and Subelement looks like a line, not like a xml-tree.
I do this:
tree = ET.parse('...
0
votes
2
answers
67
views
ElementTree cannot find one of the children elements
I have looked in other questions regarding this issue and none of them helped me. I'm parsing a XML with ElementTree and I am having problem finding a specific tag, which may be optional, while I can ...
1
vote
0
answers
109
views
Python ElementTree – how to edit XML file maintaining certain tags as self-closing?
I am editing an XML file in Python 3.10 using ElementTree.
I need certain empty elements to use self-closing tags, i.e. <a/>, whereas others need to be written out in full <b></b>. ...
1
vote
1
answer
33
views
Python to sort nested XML alongside unsorted siblings
I need to sort the "line" elements of the following sample XML with Elementtree by reqdate (desc - most recent to oldest).
<doc>
<header>
<ordernum>1234</...
0
votes
1
answer
69
views
Generate TRAIN_DATA for spacy from xml
I have xml data, which looks like this:
<item n="main"><anchor type="b" ana="regO.lemID_12" xml:id="TidB13" />Stuttgart<anchor type="e" ...
0
votes
1
answer
73
views
Get multi level first child XML Element
I want to get a first child of a XML document (without knowing the exact names of the nodes), multi levels deep.
I'm porting some code from JS to Python. This was the JS code:
let document = ...
0
votes
1
answer
283
views
'/xad' appearing in list of strings in python code
Firstly, I am a beginner, just bordering on intermediate with python, so please be patient with my approach to this problem. I was working on a web scraping mini project using lxml etree and requests (...
0
votes
2
answers
324
views
Trouble reading XML with ElementTree due to xmlns and xsi
I'm reading an XML with python and ElementTree and am struggling with xmlns and xsi tags.
The top of my XML looks like this.
<?xml version="1.0" encoding="utf-8"?>
<?xml-...
1
vote
1
answer
74
views
Inconsistency in indent function on xml.etree.ElementTree
the aim of the code is to insert XML chunks into an XML base structure to create the final XML output.
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import XMLParser
base_tree = ...
1
vote
2
answers
84
views
To print the data after replacing the given expression with the values in XML file using "ElementTree" library
Given XML file snippet is:
<?xml version="1.0" standalone="yes"?>
<event_configuration family="21" version="2">
<pqr subtype="abc"&...