<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python &#8211; Tech-Freaks.com</title>
	<atom:link href="https://www.tech-freaks.com/python/feed" rel="self" type="application/rss+xml" />
	<link>https://www.tech-freaks.com</link>
	<description></description>
	<lastBuildDate>Mon, 30 Jun 2025 02:20:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>

<image>
	<url>https://www.tech-freaks.com/wp-content/uploads/2025/07/cropped-tech-freaks-site-icon-512x512-1-32x32.png</url>
	<title>Python &#8211; Tech-Freaks.com</title>
	<link>https://www.tech-freaks.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Multiple ways to add sub elements to XML elements using Python</title>
		<link>https://www.tech-freaks.com/python/add-subelement-xml.html</link>
					<comments>https://www.tech-freaks.com/python/add-subelement-xml.html#respond</comments>
		
		<dc:creator><![CDATA[Tech Programmer]]></dc:creator>
		<pubDate>Wed, 28 Feb 2024 04:31:14 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://localhost/tfcom_wp/2024/02/28/add-subelement-xml/</guid>

					<description><![CDATA[Recently, I have been working on manipulating xmls and csvs using Python. I was amazed by how quick and easy [&#8230;]]]></description>
										<content:encoded><![CDATA[		<div class="wpulike wpulike-default " ><div class="wp_ulike_general_class wp_ulike_is_restricted"><button type="button"
					aria-label="Like Button"
					data-ulike-id="95"
					data-ulike-nonce="7c8c18820f"
					data-ulike-type="post"
					data-ulike-template="wpulike-default"
					data-ulike-display-likers=""
					data-ulike-likers-style="popover"
					class="wp_ulike_btn wp_ulike_put_image wp_post_btn_95"></button><span class="count-box wp_ulike_counter_up" data-ulike-counter-value="0"></span>			</div></div>
	<p style="text-align: left;">Recently, I have been working on manipulating xmls and csvs using Python. I was amazed by how quick and easy it is manipulate these files in Python. In this article I cover the flexibility of the python and xml.etree.ElementTree API. Let us jump into it.</p>
<h4 style="text-align: left;">Problem</h4>
<p style="text-align: left;">Given a dictionary of words and meaning in multiple language. We want to manipulate the xml and add another meaning in French language for the word <em>beauty. </em></p>
<p><span id="more-95"></span></p>
<pre class="language-markup"><code>&lt;dictionary&gt;
    &lt;definition&gt;
        &lt;word lang="en-US"&gt;beauty&lt;/word&gt;
        &lt;meaning lang="en-US"&gt;
            a combination of qualities, such as shape, color, or form, that pleases the aesthetic senses, especially the sight.
        &lt;/meaning&gt;
    &lt;/definition&gt;
&lt;/dictionary&gt;</code></pre>
<h4 style="text-align: left;">Approach 1</h4>
<p style="text-align: left;">The simplest approach is to find / reach the element of <em>definition. </em>Then add a sub-element <em>meaning </em>to the <em>definition. </em>This would look something like below.</p>
<pre class="language-python"><code>import xml.etree.ElementTree as ET

# Parse the XML file
tree = ET.parse('sample.xml')
root = tree.getroot()

beauty_meaning_fr = "La beauté est l'harmonie des formes et des couleurs qui évoque un sentiment d'émerveillement et d'admiration."

def_elements = root.findall('definition')

for definition in def_elements:
    word = definition.find('word')
    if word.text == 'beauty':
        new_meaning = ET.SubElement(definition, 'meaning')
        new_meaning.text = beauty_meaning_fr
        new_meaning.set('lang', 'fr-FR')

tree.write('output.xml')</code></pre>
<p style="text-align: left;">This approach works great and provides the desired output.</p>
<pre class="language-markup"><code>&lt;dictionary&gt;
    &lt;definition&gt;
        &lt;word lang="en-US"&gt;beauty&lt;/word&gt;
        &lt;meaning lang="en-US"&gt;
            a combination of qualities, such as shape, color, or form, that pleases the aesthetic senses, especially the sight.
        &lt;/meaning&gt;
    &lt;meaning lang="fr-FR"&gt;La beaut&amp;#233; est l'harmonie des formes et des couleurs qui &amp;#233;voque un sentiment d'&amp;#233;merveillement et d'admiration.&lt;/meaning&gt;&lt;/definition&gt;
&lt;/dictionary&gt;</code></pre>
<p style="text-align: left;">However, when I was working on my real-life problem, the above code did not add the <em>meaning </em>element for some reason. Hence, I started looking for another approach to solve my issue.</p>
<h3 style="text-align: left;">Approach 2</h3>
<p style="text-align: left;">Another option is to add a new element <em>meaning</em>, instead of a sub-element. Of course, the element will then need to added to another element. To achieve our requirement, we need to reach the <em>definition </em>element and insert or append the <em>meaning</em> element that we initially created. The below code summarizes this approach:</p>
<pre class="language-python"><code>import xml.etree.ElementTree as ET

# Parse the XML file
tree = ET.parse('sample.xml')
root = tree.getroot()

beauty_meaning_fr = "La beauté est l'harmonie des formes et des couleurs qui évoque un sentiment d'émerveillement et d'admiration."

def_elements = root.findall('definition')

for definition in def_elements:
    word = definition.find('word')
    if word.text == 'beauty':
        new_meaning = ET.Element("meaning")
        new_meaning.text = beauty_meaning_fr
        new_meaning.set('lang', 'fr-FR')
        definition.append(new_meaning)

tree.write('output1.xml')</code></pre>
<p style="text-align: left;">The code works as expected and generates the output similar to that of approach 1.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.tech-freaks.com/python/add-subelement-xml.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
