<?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>XML Archives - Be on the Right Side of Change</title>
	<atom:link href="https://blog.finxter.com/category/xml/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.finxter.com/category/xml/</link>
	<description></description>
	<lastBuildDate>Sat, 12 Nov 2022 20:26:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.finxter.com/wp-content/uploads/2020/08/cropped-cropped-finxter_nobackground-32x32.png</url>
	<title>XML Archives - Be on the Right Side of Change</title>
	<link>https://blog.finxter.com/category/xml/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Parsing XML Files in Python &#8211; 4 Simple Ways</title>
		<link>https://blog.finxter.com/parsing-xml-files-in-python-a-simple-guide/</link>
		
		<dc:creator><![CDATA[Kat McKelvie]]></dc:creator>
		<pubDate>Sat, 12 Nov 2022 14:05:43 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=883225</guid>

					<description><![CDATA[<p>Problem Formulation and Solution Overview This article will show you various ways to work with an XML file. ℹ️ XML is an acronym for Extensible Markup Language. This file type is similar to HTML. However, XML does not have pre-defined tags like HTML. Instead, a coder can define their own tags to meet specific requirements. ... <a title="Parsing XML Files in Python &#8211; 4 Simple Ways" class="read-more" href="https://blog.finxter.com/parsing-xml-files-in-python-a-simple-guide/" aria-label="Read more about Parsing XML Files in Python &#8211; 4 Simple Ways">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/parsing-xml-files-in-python-a-simple-guide/">Parsing XML Files in Python &#8211; 4 Simple Ways</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio wp-block-heading">Problem Formulation and Solution Overview</h2>



<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">This article will show you various ways to work with an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> is an acronym for E<strong>x</strong>tensible <strong>M</strong>arkup <strong>L</strong>anguage. This file type is similar to HTML. However, <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"></a><a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> does not have pre-defined tags like HTML. Instead, a coder can define their own tags to meet specific requirements.  <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"></a><a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> is a great way to transmit and share data, either locally or via the internet. This file can be parsed based on standardized <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"></a><a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> if structured correctly.</p>



<p>To make it more interesting, we have the following running scenario:</p>



<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Jan, a Bookstore Owner, wants to know the top three (3) selling Books in her store. This data is currently saved in an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> format. </p>



<hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio"/>



<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4ac.png" alt="💬" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to read in and extract data from an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> file into a Python script<em>?</em></p>



<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this by performing the following steps:</p>



<ul class="wp-block-list">
<li><strong>Method 1</strong>: Use <a rel="noreferrer noopener" href="https://pypi.org/project/xmltodict/" data-type="URL" data-id="https://pypi.org/project/xmltodict/" target="_blank"><code>xmltodict()</code></a> </li>



<li><strong>Method 2</strong>: Use <a rel="noreferrer noopener" href="https://docs.python.org/3/library/xml.dom.minidom.html" target="_blank"><code>minidom.parse()</code></a></li>



<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://docs.python.org/3/library/xml.etree.elementtree.html" data-type="URL" data-id="https://docs.python.org/3/library/xml.etree.elementtree.html" target="_blank"><code>etree</code></a></li>



<li><strong>Method 4:</strong> Use <a rel="noreferrer noopener" href="https://pypi.org/project/untangle/" data-type="URL" data-id="https://pypi.org/project/untangle/" target="_blank"><code>untangle.parse()</code></a></li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Method 1: Use xmltodict()</h2>



<p class="has-global-color-8-background-color has-background">This method uses the <a rel="noreferrer noopener" href="https://pypi.org/project/xmltodict/" data-type="URL" data-id="https://pypi.org/project/xmltodict/" target="_blank"><code>xmltodict()</code></a> function to read an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file, convert it to a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>Dictionary</code></a> and extract the data.</p>



<p>In the current working directory, create an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file called <code>books.xml</code>. Copy and paste the code snippet below into this file and save it.</p>



<pre class="wp-block-preformatted"><code>&lt;bookstore&gt;
    &lt;book&gt;
        &lt;title&gt;Surrender&lt;/title&gt;
        &lt;author&gt;Bono&lt;/author&gt;
        &lt;sales&gt;21987&lt;/sales&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Going Rogue&lt;/title&gt;
        &lt;author&gt;Janet Evanovich&lt;/author&gt;
        &lt;sales&gt;15986&lt;/sales&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Triple Cross&lt;/title&gt;
        &lt;author&gt;James Patterson&lt;/author&gt;
        &lt;sales&gt;11311&lt;/sales&gt;
    &lt;/book&gt;
&lt;/bookstore&gt;</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>In the current working directory, create a Python file called <code>books.py</code>. Copy and paste the code snippet below into this file and save it. This code reads in and parses the above <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file. If necessary, <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-xmltodict-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-xmltodict-in-python/" target="_blank">install</a> the <a rel="noreferrer noopener" href="https://pypi.org/project/xmltodict/" data-type="URL" data-id="https://pypi.org/project/xmltodict/" target="_blank"><code>xmltodict</code></a> library.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-5, 7-10" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import xmltodict

with open('books.xml', 'r') as fp:
    books_dict =  xmltodict.parse(fp.read())
    fp.close()

for i in books_dict:
    for j in books_dict[i]:
        for k in books_dict[i][j]:
            print(f'Title: {k["title"]} \t Sales: {k["sales"]}')</pre>



<p>The first line in the above code snippet imports the <a rel="noreferrer noopener" href="https://pypi.org/project/xmltodict/" data-type="URL" data-id="https://pypi.org/project/xmltodict/" target="_blank"><code>xmltodict</code></a> library. This library is needed to access and parse the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file.</p>



<p>The following highlighted section opens <code>books.xml</code> in read mode (<code>r</code>) and saves it as a File Object, fp. If fp was output to the terminal, an object similar to the one below would display.</p>



<pre class="wp-block-preformatted"><code>&lt;_io.TextIOWrapper name='books.xml' mode='r' encoding='cp1252'&gt;</code></pre>



<p>Next, the <a href="https://pypi.org/project/xmltodict/" data-type="URL" data-id="https://pypi.org/project/xmltodict/"><code>xmltodict.parse()</code></a> function is called and passed one (1) argument, <a rel="noreferrer noopener" href="https://blog.finxter.com/5-ways-to-read-a-text-file-from-a-url/" data-type="URL" data-id="https://blog.finxter.com/5-ways-to-read-a-text-file-from-a-url/" target="_blank"><code>fp.read()</code></a>, which reads in and parses the contents of the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file. The results save to <code>books_dict</code> as a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>Dictionary</code></a>, and the file is closed. The contents of <code>books_dict</code> are shown below.</p>



<pre class="wp-block-preformatted"><code>{'bookstore': 
  {'book': [{'title': Surrender', 'author': 'Bono', 'sales': '21987'}, 
              {'title': 'Going Rogue', 'author': 'Janet Evanovich', 'sales': '15986'}, 
              {'title': 'Triple Cross', 'author': 'James Patterson', 'sales': '11311'}]}}</code></pre>



<p>The final highlighted section loops through the above <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank"><code>Dictionary</code></a> and extracts each book&#8217;s <code>Title</code> and <code>Sales</code>.</p>



<pre class="wp-block-preformatted"><code>Title: Surrender         Sales: 21987
Title: Going Rogue    Sales: 15986
Title: Triple Cross      Sales: 11311</code></pre>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: The <code>\t</code> character represents the &lt;Tab&gt; key on the keyboard.</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Python Dictionary – The Ultimate Guide" width="937" height="527" src="https://www.youtube.com/embed/qX0qqEVpP5s?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Method 2: Use minidom.parse()</h2>



<p class="has-global-color-8-background-color has-background">This method uses the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/xml.dom.minidom.html" data-type="URL" data-id="https://docs.python.org/3/library/xml.dom.minidom.html" target="_blank"><code>minidom.parse()</code></a> function to read and parse an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> file. This example extracts the ID, Title and Sales for each book.</p>



<p>This example differs from Method 1 as this <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file contains an additional line at the top (<code>&lt;?xml version="1.0"?&gt;</code>) of the file and each <code>&lt;book&gt;</code> tag now has an <code>id</code> (attribute) assigned to it. </p>



<p>In the current working directory, create an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file called <code>books2.xml</code>. Copy and paste the code snippet below into this file and save it.</p>



<pre class="wp-block-preformatted"><code>&lt;?xml version="1.0"?&gt;
&lt;bookstore&gt;
	&lt;storename&gt;Jan's Best Sellers List&lt;/storename&gt;
	&lt;book id="21237"&gt;
		&lt;title&gt;Surrender&lt;/title&gt;
                &lt;author&gt;Bono&lt;/author&gt;
		&lt;sales&gt;21987&lt;/sales&gt;
	&lt;/book&gt;
	&lt;book id="21946"&gt;
		&lt;title&gt;Going Rogue&lt;/title&gt;
                &lt;author&gt;Janet Evanovich&lt;/author&gt;
		&lt;sales&gt;15986&lt;/sales&gt;
	&lt;/book&gt;
	&lt;book id="18241"&gt;
		&lt;title&gt;Triple Cross&lt;/title&gt;
                &lt;author&gt;James Patterson&lt;/author&gt;
		&lt;sales&gt;11311&lt;/sales&gt;
	&lt;/book&gt;
&lt;/bookstore&gt;</code></pre>



<p>In the current working directory, create a Python file called <code>books2.py</code>. Copy and paste the code snippet below into this file and save it. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-5, 7-13" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from xml.dom import minidom

doc = minidom.parse('books2.xml')
name = doc.getElementsByTagName('storename')[0]
books = doc.getElementsByTagName('book')

for b in books:
        bid    = b.getAttribute('id')
        title  = b.getElementsByTagName('title')[0]
        sales  = b.getElementsByTagName('sales')[0]
        print(f'{bid} {title.firstChild.data} {sales.firstChild.data}')</pre>



<p>The first line in the above code snippet imports the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/xml.dom.minidom.html" data-type="URL" data-id="https://docs.python.org/3/library/xml.dom.minidom.html" target="_blank"><code>minidom</code></a> library. This allows access to various functions to parse the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file and retrieve tags and attributes.</p>



<p>The first section of highlighted lines performs the following:</p>



<ul class="wp-block-list">
<li>Reads and parse the <code>books2.xml</code> file and saves the results to <code>doc</code>. This action creates the Object shown as (1) below.</li>



<li>Retrieves the <code>&lt;storename&gt;</code> tag and saves the results to <code>name</code>. This action creates an Object shown as (2) below.</li>



<li>Retrieves the <code>&lt;book&gt;</code> tag for each <code>book</code> and saves the results to <code>books</code>. This action creates a List of three (3) Objects: one for each book shown as (3) below.</li>
</ul>



<pre class="wp-block-preformatted"><code>(1) &lt;xml.dom.minidom.Document object at 0x0000022D764AFEE0&gt; 
(2) &lt;DOM Element: storename at 0x22d764f0ee0&gt; 
(3) [&lt;DOM Element: book at 0x22d764f3a30&gt;, 
&lt;DOM Element: book at 0x22d764f3c70&gt;, 
&lt;DOM Element: book at 0x22d764f3eb0&gt;]</code></pre>



<p>The last section of highlighted lines loop through the books Object and outputs the results to the terminal.</p>



<pre class="wp-block-preformatted"><code>21237 Surrender 21987
21946 Going Rogue 15986
18241 Triple Cross 11311</code></pre>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Python getattr() - Ultimate Guide" width="937" height="527" src="https://www.youtube.com/embed/5MXDZI3jRio?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Method 3: Use etree</h2>



<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://docs.python.org/3/library/xml.etree.elementtree.html" data-type="URL" data-id="https://docs.python.org/3/library/xml.etree.elementtree.html" target="_blank"><code>etree</code></a> to read in and parses an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> file. This example extracts the Title and Sales data for each book.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The <code>etree</code> considers the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> file as a tree structure. Each element represents a node of said tree. Accessing elements is done on an element level.</p>



<p>This example reads in and parses the <code>books2.xml</code> file created earlier.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3,4, 6-10" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import xml.etree.ElementTree as ET

xml_data = ET.parse('books2.xml')
root = xml_data.getroot()

for books in root.findall('book'):
    title = books.find('title').text
    author = books.find('author').text
    sales = books.find('sales').text
    print(title, author, sales)</pre>



<p>The first line in the above code snippet imports the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/xml.etree.elementtree.html" data-type="URL" data-id="https://docs.python.org/3/library/xml.etree.elementtree.html" target="_blank"><code>etree</code></a> library. This allows access to all nodes of the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> <code>&lt;tag&gt;</code> structure.</p>



<p>The following line reads in and parses <code>books2.xml</code>. The results save as an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> Object to <code>xml_data</code>. If output to the terminal, an Object similar to the one below displays.</p>



<pre class="wp-block-preformatted"><code>&lt;Element 'bookstore' at 0x000001E45E9442C0&gt;</code></pre>



<p>The following highlighted section uses a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop to iterate through each <code>&lt;book&gt;</code> tag, extracting the <code>&lt;title&gt;</code>, <code>&lt;author&gt;</code> and <code>&lt;sales&gt;</code> tags for each book and outputting them to the terminal.</p>



<pre class="wp-block-preformatted"><code>Surrender Bono 21987
Going Rogue Janet Evanovich 15986
Triple Cross James Patterson 11311</code></pre>



<p>To retrieve the attribute of the <code>&lt;book&gt;</code> tag, run the following code.</p>



<p>This code extracts the <code>id</code> attribute from each <code>&lt;book&gt;</code> tag and outputs it to the terminal.</p>



<pre class="wp-block-preformatted"><code>{'id': '21237'}
{'id': '21946'}
{'id': '18241'}</code></pre>



<p>To extract the values, run the following code.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for id in root.iter('book'):
    vals = id.attrib.values()
    for v in vals:
        print(vals)</pre>



<pre class="wp-block-preformatted"><code>21237
21946
18241</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Method 4: Use untangle.parse()</h2>



<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://pypi.org/project/untangle/" data-type="URL" data-id="https://pypi.org/project/untangle/" target="_blank"><code>untangle.parse()</code></a> to parse an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> string.</p>



<p>This example reads in and parses the <code>books3.xml</code> file shown below. If necessary, install the <a rel="noreferrer noopener" href="https://pypi.org/project/untangle/" data-type="URL" data-id="https://pypi.org/project/untangle/" target="_blank"><code>untangle</code></a> library.</p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> The <a rel="noreferrer noopener" href="https://pypi.org/project/untangle/" data-type="URL" data-id="https://pypi.org/project/untangle/" target="_blank"><code>untangle</code></a> library converts an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank">XML</a> file to a Python object. This is a good option when you have a group of items, such as book names.</p>



<p>In the current working directory, create an <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file called <code>books3.xml</code>. Copy and paste the code snippet below into this file and save it. If necessary, <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-xmltodict-in-python/" target="_blank">install</a> the <a href="https://pypi.org/project/untangle/" data-type="URL" data-id="https://pypi.org/project/untangle/" target="_blank" rel="noreferrer noopener"><code>untangle</code></a> library.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0"?>
&lt;root>
    &lt;book name="Surrender"/>
    &lt;book name="Going Rogue"/>
    &lt;book name="Triple Cross"/>
&lt;/root></pre>



<p>In the current working directory, create a Python file called <code>books3.py</code>. Copy and paste the code snippet below into this file and save it. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-4,6-7" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import untangle

book_obj = untangle.parse('books3.xml')
books = ','.join([book['name'] for book in book_obj.root.book])

for b in books.split(','):
    print(b)</pre>



<p>The first line in the above code snippet imports the <a rel="noreferrer noopener" href="https://pypi.org/project/untangle/" data-type="URL" data-id="https://pypi.org/project/untangle/" target="_blank"><code>untangle</code></a> library allowing access to the <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> file structure.</p>



<p>The following line reads in and parses the <code>books3.xml</code> file. The results save to <code>book_obj</code>. </p>



<p>The next line calls the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-join/" data-type="URL" data-id="https://blog.finxter.com/python-string-join/" target="_blank"><code>join()</code></a> function and passes it one (1) argument: <a rel="noreferrer noopener" href="https://blog.finxter.com/list-comprehension/" data-type="URL" data-id="https://blog.finxter.com/list-comprehension/" target="_blank">List Comprehension</a>. This code iterates through and retrieves the name of each book and saves the results to <code>books</code>. If output to the terminal, the following displays:</p>



<pre class="wp-block-preformatted"><code> Surrender,Going Rogue,Triple Cross</code></pre>



<p>The next line instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop, iterates through each book name, and sends it to the terminal.</p>



<pre class="wp-block-preformatted"><code>Surrender
Going Rogue
Triple Cross</code></pre>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Python One-Liner Trick 9 - Nested List Comprehension" width="937" height="527" src="https://www.youtube.com/embed/aBC0VhpXkOQ?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Summary</h2>



<p>This article has shown four (4) ways to work with <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/XML/XML_introduction" target="_blank"><code>XML</code></a> files to select the best fit for your coding requirements.</p>



<p>Good Luck &amp; Happy Coding!</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading">Programmer Humor &#8211; Blockchain</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="280" height="394" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png" alt="" class="wp-image-457795" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png 280w, https://blog.finxter.com/wp-content/uploads/2022/07/image-31-213x300.png 213w" sizes="auto, (max-width: 280px) 100vw, 280px" /><figcaption><em>&#8220;Blockchains are like grappling hooks, in that it&#8217;s extremely cool when you encounter a problem for which they&#8217;re the right solution, but it happens way too rarely in real life.&#8221;</em> <strong>source </strong>&#8211; <a href="https://imgs.xkcd.com/comics/blockchain.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/blockchain.png" target="_blank" rel="noreferrer noopener">xkcd</a></figcaption></figure>
</div><p>The post <a href="https://blog.finxter.com/parsing-xml-files-in-python-a-simple-guide/">Parsing XML Files in Python &#8211; 4 Simple Ways</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python &#8211; How to Convert KML to CSV?</title>
		<link>https://blog.finxter.com/python-how-to-convert-kml-to-csv/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Thu, 18 Aug 2022 15:19:59 +0000</pubDate>
				<category><![CDATA[BeautifulSoup]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[Input/Output]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=587850</guid>

					<description><![CDATA[<p>What is KML? ℹ️ Definition: The Keyhole Markup Language (KML) is a file format for displaying geographic data in Google Earth or other so-called &#8220;Earth Browsers&#8221;. Similarly to XML, KML uses a tag-based structure with nested elements and attributes. How to Convert KML to CSV in Python? You can convert a .kml to a .csv ... <a title="Python &#8211; How to Convert KML to CSV?" class="read-more" href="https://blog.finxter.com/python-how-to-convert-kml-to-csv/" aria-label="Read more about Python &#8211; How to Convert KML to CSV?">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/python-how-to-convert-kml-to-csv/">Python &#8211; How to Convert KML to CSV?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">What is KML?</h2>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="352" height="263" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-45.png" alt="" class="wp-image-587952" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-45.png 352w, https://blog.finxter.com/wp-content/uploads/2022/08/image-45-300x224.png 300w" sizes="auto, (max-width: 352px) 100vw, 352px" /></figure>
</div>


<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Definition</strong>: The <a href="https://developers.google.com/kml/documentation/kml_tut" data-type="URL" data-id="https://developers.google.com/kml/documentation/kml_tut" target="_blank" rel="noreferrer noopener">Keyhole Markup Language</a> (KML) is a file format for displaying geographic data in Google Earth or other so-called &#8220;Earth Browsers&#8221;. Similarly to XML, KML uses a tag-based structure with nested elements and attributes. </p>



<h2 class="wp-block-heading">How to Convert KML to CSV in Python?</h2>



<p class="has-global-color-8-background-color has-background">You can convert a <code>.kml</code> to a <code>.csv</code> file in Python by using the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-beautifulsoup-xml-to-dict-json-dataframe-csv/" data-type="post" data-id="474965" target="_blank">BeautifulSoup</a> and the <code>csv</code> libraries. You use the former to read the XML-structured KML file and the latter to write the CSV file row by row. </p>



<p>Here&#8217;s the code example inspired but modified from <a href="https://gist.github.com/mciantyre/32ff2c2d5cd9515c1ee7" data-type="URL" data-id="https://gist.github.com/mciantyre/32ff2c2d5cd9515c1ee7" target="_blank" rel="noreferrer noopener">this</a> GitHub repository. You can copy&amp;paste it in the directory where your KML file resides and change the input and output filenames at the beginning to convert your own KML to a CSV in Python:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from bs4 import BeautifulSoup
import csv


infile = 'my_file.kml'
outfile = 'my_file.csv'


with open(infile, 'r') as f:
    s = BeautifulSoup(f, 'xml')
    
    with open(outfile, 'wb') as csvfile:
        writer = csv.writer(csvfile)

        for coords in s.find_all('coordinates'):
            
            # Take coordinate string from KML and break it up into [Lat,Lon,Lat,Lon...] to get CSV row
            space_splits = coords.string.split(" ")
            row = []
            
            for split in space_splits[1:]:
                # Note: because of the space between &lt;coordinates>" "-80.123, we slice [1:]
                comma_split = split.split(',')

                # lattitude
                row.append(comma_split[1])
                
                # longitude
                row.append(comma_split[0])
            
            writer.writerow(row)
</pre>



<h2 class="wp-block-heading">Example Conversion</h2>



<p>We use the following <a href="https://developers.google.com/kml/documentation/kml_tut" data-type="URL" data-id="https://developers.google.com/kml/documentation/kml_tut" target="_blank" rel="noreferrer noopener">sample</a> KML file as <code>'my_file.kml'</code>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;kml xmlns="http://www.opengis.net/kml/2.2">
  &lt;Document>
    &lt;name>KML Samples&lt;/name>
    &lt;open>1&lt;/open>
    &lt;description>Unleash your creativity with the help of these examples!&lt;/description>
    &lt;Style id="downArrowIcon">
      &lt;IconStyle>
        &lt;Icon>
          &lt;href>http://maps.google.com/mapfiles/kml/pal4/icon28.png&lt;/href>
        &lt;/Icon>
      &lt;/IconStyle>
    &lt;/Style>
    &lt;Style id="globeIcon">
      &lt;IconStyle>
        &lt;Icon>
          &lt;href>http://maps.google.com/mapfiles/kml/pal3/icon19.png&lt;/href>
        &lt;/Icon>
      &lt;/IconStyle>
      &lt;LineStyle>
        &lt;width>2&lt;/width>
      &lt;/LineStyle>
    &lt;/Style>
    &lt;Style id="transPurpleLineGreenPoly">
      &lt;LineStyle>
        &lt;color>7fff00ff&lt;/color>
        &lt;width>4&lt;/width>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>7f00ff00&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="yellowLineGreenPoly">
      &lt;LineStyle>
        &lt;color>7f00ffff&lt;/color>
        &lt;width>4&lt;/width>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>7f00ff00&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="thickBlackLine">
      &lt;LineStyle>
        &lt;color>87000000&lt;/color>
        &lt;width>10&lt;/width>
      &lt;/LineStyle>
    &lt;/Style>
    &lt;Style id="redLineBluePoly">
      &lt;LineStyle>
        &lt;color>ff0000ff&lt;/color>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>ffff0000&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="blueLineRedPoly">
      &lt;LineStyle>
        &lt;color>ffff0000&lt;/color>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>ff0000ff&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="transRedPoly">
      &lt;LineStyle>
        &lt;width>1.5&lt;/width>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>7d0000ff&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="transBluePoly">
      &lt;LineStyle>
        &lt;width>1.5&lt;/width>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>7dff0000&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="transGreenPoly">
      &lt;LineStyle>
        &lt;width>1.5&lt;/width>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>7d00ff00&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="transYellowPoly">
      &lt;LineStyle>
        &lt;width>1.5&lt;/width>
      &lt;/LineStyle>
      &lt;PolyStyle>
        &lt;color>7d00ffff&lt;/color>
      &lt;/PolyStyle>
    &lt;/Style>
    &lt;Style id="noDrivingDirections">
      &lt;BalloonStyle>
        &lt;text>&lt;![CDATA[
          &lt;b>$[name]&lt;/b>
          &lt;br />&lt;br />
          $[description]
        ]]&gt;&lt;/text>
      &lt;/BalloonStyle>
    &lt;/Style>
    &lt;Folder>
      &lt;name>Placemarks&lt;/name>
      &lt;description>These are just some of the different kinds of placemarks with
        which you can mark your favorite places&lt;/description>
      &lt;LookAt>
        &lt;longitude>-122.0839597145766&lt;/longitude>
        &lt;latitude>37.42222904525232&lt;/latitude>
        &lt;altitude>0&lt;/altitude>
        &lt;heading>-148.4122922628044&lt;/heading>
        &lt;tilt>40.5575073395506&lt;/tilt>
        &lt;range>500.6566641072245&lt;/range>
      &lt;/LookAt>
      &lt;Placemark>
        &lt;name>Simple placemark&lt;/name>
        &lt;description>Attached to the ground. Intelligently places itself at the
          height of the underlying terrain.&lt;/description>
        &lt;Point>
          &lt;coordinates>-122.0822035425683,37.42228990140251,0&lt;/coordinates>
        &lt;/Point>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Floating placemark&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Floats a defined distance above the ground.&lt;/description>
        &lt;LookAt>
          &lt;longitude>-122.0839597145766&lt;/longitude>
          &lt;latitude>37.42222904525232&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-148.4122922628044&lt;/heading>
          &lt;tilt>40.5575073395506&lt;/tilt>
          &lt;range>500.6566641072245&lt;/range>
        &lt;/LookAt>
        &lt;styleUrl>#downArrowIcon&lt;/styleUrl>
        &lt;Point>
          &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
          &lt;coordinates>-122.084075,37.4220033612141,50&lt;/coordinates>
        &lt;/Point>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Extruded placemark&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Tethered to the ground by a customizable
          &amp;quot;tail&amp;quot;&lt;/description>
        &lt;LookAt>
          &lt;longitude>-122.0845787421525&lt;/longitude>
          &lt;latitude>37.42215078737763&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-148.4126684946234&lt;/heading>
          &lt;tilt>40.55750733918048&lt;/tilt>
          &lt;range>365.2646606980322&lt;/range>
        &lt;/LookAt>
        &lt;styleUrl>#globeIcon&lt;/styleUrl>
        &lt;Point>
          &lt;extrude>1&lt;/extrude>
          &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
          &lt;coordinates>-122.0857667006183,37.42156927867553,50&lt;/coordinates>
        &lt;/Point>
      &lt;/Placemark>
    &lt;/Folder>
    &lt;Folder>
      &lt;name>Styles and Markup&lt;/name>
      &lt;visibility>0&lt;/visibility>
      &lt;description>With KML it is easy to create rich, descriptive markup to
        annotate and enrich your placemarks&lt;/description>
      &lt;LookAt>
        &lt;longitude>-122.0845787422371&lt;/longitude>
        &lt;latitude>37.42215078726837&lt;/latitude>
        &lt;altitude>0&lt;/altitude>
        &lt;heading>-148.4126777488172&lt;/heading>
        &lt;tilt>40.55750733930874&lt;/tilt>
        &lt;range>365.2646826292919&lt;/range>
      &lt;/LookAt>
      &lt;styleUrl>#noDrivingDirections&lt;/styleUrl>
      &lt;Document>
        &lt;name>Highlighted Icon&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Place your mouse over the icon to see it display the new
          icon&lt;/description>
        &lt;LookAt>
          &lt;longitude>-122.0856552124024&lt;/longitude>
          &lt;latitude>37.4224281311035&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>0&lt;/heading>
          &lt;tilt>0&lt;/tilt>
          &lt;range>265.8520424250024&lt;/range>
        &lt;/LookAt>
        &lt;Style id="highlightPlacemark">
          &lt;IconStyle>
            &lt;Icon>
              &lt;href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png&lt;/href>
            &lt;/Icon>
          &lt;/IconStyle>
        &lt;/Style>
        &lt;Style id="normalPlacemark">
          &lt;IconStyle>
            &lt;Icon>
              &lt;href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png&lt;/href>
            &lt;/Icon>
          &lt;/IconStyle>
        &lt;/Style>
        &lt;StyleMap id="exampleStyleMap">
          &lt;Pair>
            &lt;key>normal&lt;/key>
            &lt;styleUrl>#normalPlacemark&lt;/styleUrl>
          &lt;/Pair>
          &lt;Pair>
            &lt;key>highlight&lt;/key>
            &lt;styleUrl>#highlightPlacemark&lt;/styleUrl>
          &lt;/Pair>
        &lt;/StyleMap>
        &lt;Placemark>
          &lt;name>Roll over this icon&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#exampleStyleMap&lt;/styleUrl>
          &lt;Point>
            &lt;coordinates>-122.0856545755255,37.42243077405461,0&lt;/coordinates>
          &lt;/Point>
        &lt;/Placemark>
      &lt;/Document>
      &lt;Placemark>
        &lt;name>Descriptive HTML&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>&lt;![CDATA[Click on the blue link!&lt;br>&lt;br>
Placemark descriptions can be enriched by using many standard HTML tags.&lt;br>
For example:
&lt;hr>
Styles:&lt;br>
&lt;i>Italics&lt;/i>, 
&lt;b>Bold&lt;/b>, 
&lt;u>Underlined&lt;/u>, 
&lt;s>Strike Out&lt;/s>, 
subscript&lt;sub>subscript&lt;/sub>, 
superscript&lt;sup>superscript&lt;/sup>, 
&lt;big>Big&lt;/big>, 
&lt;small>Small&lt;/small>, 
&lt;tt>Typewriter&lt;/tt>, 
&lt;em>Emphasized&lt;/em>, 
&lt;strong>Strong&lt;/strong>, 
&lt;code>Code&lt;/code>
&lt;hr>
Fonts:&lt;br> 
&lt;font color="red">red by name&lt;/font>, 
&lt;font color="#408010">leaf green by hexadecimal RGB&lt;/font>
&lt;br>
&lt;font size=1>size 1&lt;/font>, 
&lt;font size=2>size 2&lt;/font>, 
&lt;font size=3>size 3&lt;/font>, 
&lt;font size=4>size 4&lt;/font>, 
&lt;font size=5>size 5&lt;/font>, 
&lt;font size=6>size 6&lt;/font>, 
&lt;font size=7>size 7&lt;/font>
&lt;br>
&lt;font face=times>Times&lt;/font>, 
&lt;font face=verdana>Verdana&lt;/font>, 
&lt;font face=arial>Arial&lt;/font>&lt;br>
&lt;hr>
Links: 
&lt;br>
&lt;a href="http://earth.google.com/">Google Earth!&lt;/a>
&lt;br>
 or:  Check out our website at www.google.com
&lt;hr>
Alignment:&lt;br>
&lt;p align=left>left&lt;/p>
&lt;p align=center>center&lt;/p>
&lt;p align=right>right&lt;/p>
&lt;hr>
Ordered Lists:&lt;br>
&lt;ol>&lt;li>First&lt;/li>&lt;li>Second&lt;/li>&lt;li>Third&lt;/li>&lt;/ol>
&lt;ol type="a">&lt;li>First&lt;/li>&lt;li>Second&lt;/li>&lt;li>Third&lt;/li>&lt;/ol>
&lt;ol type="A">&lt;li>First&lt;/li>&lt;li>Second&lt;/li>&lt;li>Third&lt;/li>&lt;/ol>
&lt;hr>
Unordered Lists:&lt;br>
&lt;ul>&lt;li>A&lt;/li>&lt;li>B&lt;/li>&lt;li>C&lt;/li>&lt;/ul>
&lt;ul type="circle">&lt;li>A&lt;/li>&lt;li>B&lt;/li>&lt;li>C&lt;/li>&lt;/ul>
&lt;ul type="square">&lt;li>A&lt;/li>&lt;li>B&lt;/li>&lt;li>C&lt;/li>&lt;/ul>
&lt;hr>
Definitions:&lt;br>
&lt;dl>
&lt;dt>Google:&lt;/dt>&lt;dd>The best thing since sliced bread&lt;/dd>
&lt;/dl>
&lt;hr>
Centered:&lt;br>&lt;center>
Time present and time past&lt;br>
Are both perhaps present in time future,&lt;br>
And time future contained in time past.&lt;br>
If all time is eternally present&lt;br>
All time is unredeemable.&lt;br>
&lt;/center>
&lt;hr>
Block Quote:
&lt;br>
&lt;blockquote>
We shall not cease from exploration&lt;br>
And the end of all our exploring&lt;br>
Will be to arrive where we started&lt;br>
And know the place for the first time.&lt;br>
&lt;i>-- T.S. Eliot&lt;/i>
&lt;/blockquote>
&lt;br>
&lt;hr>
Headings:&lt;br>
&lt;h1>Header 1&lt;/h1>
&lt;h2>Header 2&lt;/h2>
&lt;h3>Header 3&lt;/h3>
&lt;h3>Header 4&lt;/h4>
&lt;h3>Header 5&lt;/h5>
&lt;hr>
Images:&lt;br>
&lt;i>Remote image&lt;/i>&lt;br>
&lt;img src="//developers.google.com/kml/documentation/images/googleSample.png">&lt;br>
&lt;i>Scaled image&lt;/i>&lt;br>
&lt;img src="//developers.google.com/kml/documentation/images/googleSample.png" width=100>&lt;br>
&lt;hr>
Simple Tables:&lt;br>
&lt;table border="1" padding="1">
&lt;tr>&lt;td>1&lt;/td>&lt;td>2&lt;/td>&lt;td>3&lt;/td>&lt;td>4&lt;/td>&lt;td>5&lt;/td>&lt;/tr>
&lt;tr>&lt;td>a&lt;/td>&lt;td>b&lt;/td>&lt;td>c&lt;/td>&lt;td>d&lt;/td>&lt;td>e&lt;/td>&lt;/tr>
&lt;/table>
&lt;br>
[Did you notice that double-clicking on the placemark doesn't cause the viewer to take you anywhere? This is because it is possible to directly author a "placeless placemark". If you look at the code for this example, you will see that it has neither a point coordinate nor a LookAt element.]]]&gt;&lt;/description>
      &lt;/Placemark>
    &lt;/Folder>
    &lt;Folder>
      &lt;name>Ground Overlays&lt;/name>
      &lt;visibility>0&lt;/visibility>
      &lt;description>Examples of ground overlays&lt;/description>
      &lt;GroundOverlay>
        &lt;name>Large-scale overlay on terrain&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Overlay shows Mount Etna erupting on July 13th, 2001.&lt;/description>
        &lt;LookAt>
          &lt;longitude>15.02468937557116&lt;/longitude>
          &lt;latitude>37.67395167941667&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-16.5581842842829&lt;/heading>
          &lt;tilt>58.31228652890705&lt;/tilt>
          &lt;range>30350.36838438907&lt;/range>
        &lt;/LookAt>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/etna.jpg&lt;/href>
        &lt;/Icon>
        &lt;LatLonBox>
          &lt;north>37.91904192681665&lt;/north>
          &lt;south>37.46543388598137&lt;/south>
          &lt;east>15.35832653742206&lt;/east>
          &lt;west>14.60128369746704&lt;/west>
          &lt;rotation>-0.1556640799496235&lt;/rotation>
        &lt;/LatLonBox>
      &lt;/GroundOverlay>
    &lt;/Folder>
    &lt;Folder>
      &lt;name>Screen Overlays&lt;/name>
      &lt;visibility>0&lt;/visibility>
      &lt;description>Screen overlays have to be authored directly in KML. These
        examples illustrate absolute and dynamic positioning in screen space.&lt;/description>
      &lt;ScreenOverlay>
        &lt;name>Simple crosshairs&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>This screen overlay uses fractional positioning to put the
          image in the exact center of the screen&lt;/description>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/crosshairs.png&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0.5" y="0.5" xunits="fraction" yunits="fraction"/>
        &lt;size x="0" y="0" xunits="pixels" yunits="pixels"/>
      &lt;/ScreenOverlay>
      &lt;ScreenOverlay>
        &lt;name>Absolute Positioning: Top left&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/top_left.jpg&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;size x="0" y="0" xunits="fraction" yunits="fraction"/>
      &lt;/ScreenOverlay>
      &lt;ScreenOverlay>
        &lt;name>Absolute Positioning: Top right&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/top_right.jpg&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="1" y="1" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="1" y="1" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;size x="0" y="0" xunits="fraction" yunits="fraction"/>
      &lt;/ScreenOverlay>
      &lt;ScreenOverlay>
        &lt;name>Absolute Positioning: Bottom left&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/bottom_left.jpg&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="0" y="-1" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;size x="0" y="0" xunits="fraction" yunits="fraction"/>
      &lt;/ScreenOverlay>
      &lt;ScreenOverlay>
        &lt;name>Absolute Positioning: Bottom right&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/bottom_right.jpg&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="1" y="-1" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="1" y="0" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;size x="0" y="0" xunits="fraction" yunits="fraction"/>
      &lt;/ScreenOverlay>
      &lt;ScreenOverlay>
        &lt;name>Dynamic Positioning: Top of screen&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/dynamic_screenoverlay.jpg&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;size x="1" y="0.2" xunits="fraction" yunits="fraction"/>
      &lt;/ScreenOverlay>
      &lt;ScreenOverlay>
        &lt;name>Dynamic Positioning: Right of screen&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;Icon>
          &lt;href>http://developers.google.com/kml/documentation/images/dynamic_right.jpg&lt;/href>
        &lt;/Icon>
        &lt;overlayXY x="1" y="1" xunits="fraction" yunits="fraction"/>
        &lt;screenXY x="1" y="1" xunits="fraction" yunits="fraction"/>
        &lt;rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
        &lt;size x="0" y="1" xunits="fraction" yunits="fraction"/>
      &lt;/ScreenOverlay>
    &lt;/Folder>
    &lt;Folder>
      &lt;name>Paths&lt;/name>
      &lt;visibility>0&lt;/visibility>
      &lt;description>Examples of paths. Note that the tessellate tag is by default
        set to 0. If you want to create tessellated lines, they must be authored
        (or edited) directly in KML.&lt;/description>
      &lt;Placemark>
        &lt;name>Tessellated&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>&lt;![CDATA[If the &lt;tessellate> tag has a value of 1, the line will contour to the underlying terrain]]&gt;&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.0822680013139&lt;/longitude>
          &lt;latitude>36.09825589333556&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>103.8120432044965&lt;/heading>
          &lt;tilt>62.04855796276328&lt;/tilt>
          &lt;range>2889.145007690472&lt;/range>
        &lt;/LookAt>
        &lt;LineString>
          &lt;tessellate>1&lt;/tessellate>
          &lt;coordinates> -112.0814237830345,36.10677870477137,0
            -112.0870267752693,36.0905099328766,0 &lt;/coordinates>
        &lt;/LineString>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Untessellated&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>&lt;![CDATA[If the &lt;tessellate> tag has a value of 0, the line follow a simple straight-line path from point to point]]&gt;&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.0822680013139&lt;/longitude>
          &lt;latitude>36.09825589333556&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>103.8120432044965&lt;/heading>
          &lt;tilt>62.04855796276328&lt;/tilt>
          &lt;range>2889.145007690472&lt;/range>
        &lt;/LookAt>
        &lt;LineString>
          &lt;tessellate>0&lt;/tessellate>
          &lt;coordinates> -112.080622229595,36.10673460007995,0
            -112.085242575315,36.09049598612422,0 &lt;/coordinates>
        &lt;/LineString>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Absolute&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Transparent purple line&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.2719329043177&lt;/longitude>
          &lt;latitude>36.08890633450894&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-106.8161545998597&lt;/heading>
          &lt;tilt>44.60763714063257&lt;/tilt>
          &lt;range>2569.386744398339&lt;/range>
        &lt;/LookAt>
        &lt;styleUrl>#transPurpleLineGreenPoly&lt;/styleUrl>
        &lt;LineString>
          &lt;tessellate>1&lt;/tessellate>
          &lt;altitudeMode>absolute&lt;/altitudeMode>
          &lt;coordinates> -112.265654928602,36.09447672602546,2357
            -112.2660384528238,36.09342608838671,2357
            -112.2668139013453,36.09251058776881,2357
            -112.2677826834445,36.09189827357996,2357
            -112.2688557510952,36.0913137941187,2357
            -112.2694810717219,36.0903677207521,2357
            -112.2695268555611,36.08932171487285,2357
            -112.2690144567276,36.08850916060472,2357
            -112.2681528815339,36.08753813597956,2357
            -112.2670588176031,36.08682685262568,2357
            -112.2657374587321,36.08646312301303,2357 &lt;/coordinates>
        &lt;/LineString>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Absolute Extruded&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Transparent green wall with yellow outlines&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.2643334742529&lt;/longitude>
          &lt;latitude>36.08563154742419&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-125.7518698668815&lt;/heading>
          &lt;tilt>44.61038665812578&lt;/tilt>
          &lt;range>4451.842204068102&lt;/range>
        &lt;/LookAt>
        &lt;styleUrl>#yellowLineGreenPoly&lt;/styleUrl>
        &lt;LineString>
          &lt;extrude>1&lt;/extrude>
          &lt;tessellate>1&lt;/tessellate>
          &lt;altitudeMode>absolute&lt;/altitudeMode>
          &lt;coordinates> -112.2550785337791,36.07954952145647,2357
            -112.2549277039738,36.08117083492122,2357
            -112.2552505069063,36.08260761307279,2357
            -112.2564540158376,36.08395660588506,2357
            -112.2580238976449,36.08511401044813,2357
            -112.2595218489022,36.08584355239394,2357
            -112.2608216347552,36.08612634548589,2357
            -112.262073428656,36.08626019085147,2357
            -112.2633204928495,36.08621519860091,2357
            -112.2644963846444,36.08627897945274,2357
            -112.2656969554589,36.08649599090644,2357 &lt;/coordinates>
        &lt;/LineString>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Relative&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Black line (10 pixels wide), height tracks terrain&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.2580438551384&lt;/longitude>
          &lt;latitude>36.1072674824385&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>4.947421249553717&lt;/heading>
          &lt;tilt>44.61324882043339&lt;/tilt>
          &lt;range>2927.61105910266&lt;/range>
        &lt;/LookAt>
        &lt;styleUrl>#thickBlackLine&lt;/styleUrl>
        &lt;LineString>
          &lt;tessellate>1&lt;/tessellate>
          &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
          &lt;coordinates> -112.2532845153347,36.09886943729116,645
            -112.2540466121145,36.09919570465255,645
            -112.254734666947,36.09984998366178,645
            -112.255493345654,36.10051310621746,645
            -112.2563157098468,36.10108441943419,645
            -112.2568033076439,36.10159722088088,645
            -112.257494011321,36.10204323542867,645
            -112.2584106072308,36.10229131995655,645
            -112.2596588987972,36.10240001286358,645
            -112.2610581199487,36.10213176873407,645
            -112.2626285262793,36.10157011437219,645 &lt;/coordinates>
        &lt;/LineString>
      &lt;/Placemark>
      &lt;Placemark>
        &lt;name>Relative Extruded&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Opaque blue walls with red outline, height tracks terrain&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.2683594333433&lt;/longitude>
          &lt;latitude>36.09884362144909&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-72.24271551768405&lt;/heading>
          &lt;tilt>44.60855445139561&lt;/tilt>
          &lt;range>2184.193522571467&lt;/range>
        &lt;/LookAt>
        &lt;styleUrl>#redLineBluePoly&lt;/styleUrl>
        &lt;LineString>
          &lt;extrude>1&lt;/extrude>
          &lt;tessellate>1&lt;/tessellate>
          &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
          &lt;coordinates> -112.2656634181359,36.09445214722695,630
            -112.2652238941097,36.09520916122063,630
            -112.2645079986395,36.09580763864907,630
            -112.2638827428817,36.09628572284063,630
            -112.2635746835406,36.09679275951239,630
            -112.2635711822407,36.09740038871899,630
            -112.2640296531825,36.09804913435539,630
            -112.264327720538,36.09880337400301,630
            -112.2642436562271,36.09963644790288,630
            -112.2639148687042,36.10055381117246,630
            -112.2626894973474,36.10149062823369,630 &lt;/coordinates>
        &lt;/LineString>
      &lt;/Placemark>
    &lt;/Folder>
    &lt;Folder>
      &lt;name>Polygons&lt;/name>
      &lt;visibility>0&lt;/visibility>
      &lt;description>Examples of polygon shapes&lt;/description>
      &lt;Folder>
        &lt;name>Google Campus&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>A collection showing how easy it is to create 3-dimensional
          buildings&lt;/description>
        &lt;LookAt>
          &lt;longitude>-122.084120030116&lt;/longitude>
          &lt;latitude>37.42174011925477&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-34.82469740081282&lt;/heading>
          &lt;tilt>53.454348562403&lt;/tilt>
          &lt;range>276.7870053764046&lt;/range>
        &lt;/LookAt>
        &lt;Placemark>
          &lt;name>Building 40&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#transRedPoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -122.0848938459612,37.42257124044786,17
                  -122.0849580979198,37.42211922626856,17
                  -122.0847469573047,37.42207183952619,17
                  -122.0845725380962,37.42209006729676,17
                  -122.0845954886723,37.42215932700895,17
                  -122.0838521118269,37.42227278564371,17
                  -122.083792243335,37.42203539112084,17
                  -122.0835076656616,37.42209006957106,17
                  -122.0834709464152,37.42200987395161,17
                  -122.0831221085748,37.4221046494946,17
                  -122.0829247374572,37.42226503990386,17
                  -122.0829339169385,37.42231242843094,17
                  -122.0833837359737,37.42225046087618,17
                  -122.0833607854248,37.42234159228745,17
                  -122.0834204551642,37.42237075460644,17
                  -122.083659133885,37.42251292011001,17
                  -122.0839758438952,37.42265873093781,17
                  -122.0842374743331,37.42265143972521,17
                  -122.0845036949503,37.4226514386435,17
                  -122.0848020460801,37.42261133916315,17
                  -122.0847882750515,37.42256395055121,17
                  -122.0848938459612,37.42257124044786,17 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
        &lt;Placemark>
          &lt;name>Building 41&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#transBluePoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -122.0857412771483,37.42227033155257,17
                  -122.0858169768481,37.42231408832346,17
                  -122.085852582875,37.42230337469744,17
                  -122.0858799945639,37.42225686138789,17
                  -122.0858860101409,37.4222311076138,17
                  -122.0858069157288,37.42220250173855,17
                  -122.0858379542653,37.42214027058678,17
                  -122.0856732640519,37.42208690214408,17
                  -122.0856022926407,37.42214885429042,17
                  -122.0855902778436,37.422128290487,17
                  -122.0855841672237,37.42208171967246,17
                  -122.0854852065741,37.42210455874995,17
                  -122.0855067264352,37.42214267949824,17
                  -122.0854430712915,37.42212783846172,17
                  -122.0850990714904,37.42251282407603,17
                  -122.0856769818632,37.42281815323651,17
                  -122.0860162273783,37.42244918858722,17
                  -122.0857260327004,37.42229239604253,17
                  -122.0857412771483,37.42227033155257,17 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
        &lt;Placemark>
          &lt;name>Building 42&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#transGreenPoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -122.0857862287242,37.42136208886969,25
                  -122.0857312990603,37.42136935989481,25
                  -122.0857312992918,37.42140934910903,25
                  -122.0856077073679,37.42138390166565,25
                  -122.0855802426516,37.42137299550869,25
                  -122.0852186221971,37.42137299504316,25
                  -122.0852277765639,37.42161656508265,25
                  -122.0852598189347,37.42160565894403,25
                  -122.0852598185499,37.42168200156,25
                  -122.0852369311478,37.42170017860346,25
                  -122.0852643957828,37.42176197982575,25
                  -122.0853239032746,37.42176198013907,25
                  -122.0853559454324,37.421852864452,25
                  -122.0854108752463,37.42188921823734,25
                  -122.0854795379357,37.42189285337048,25
                  -122.0855436229819,37.42188921797546,25
                  -122.0856260178042,37.42186013499926,25
                  -122.085937287963,37.42186013453605,25
                  -122.0859428718666,37.42160898590042,25
                  -122.0859655469861,37.42157992759144,25
                  -122.0858640462341,37.42147115002957,25
                  -122.0858548911215,37.42140571326184,25
                  -122.0858091162768,37.4214057134039,25
                  -122.0857862287242,37.42136208886969,25 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
        &lt;Placemark>
          &lt;name>Building 43&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#transYellowPoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -122.0844371128284,37.42177253003091,19
                  -122.0845118855746,37.42191111542896,19
                  -122.0850470999805,37.42178755121535,19
                  -122.0850719913391,37.42143663023161,19
                  -122.084916406232,37.42137237822116,19
                  -122.0842193868167,37.42137237801626,19
                  -122.08421938659,37.42147617161496,19
                  -122.0838086419991,37.4214613409357,19
                  -122.0837899728564,37.42131306410796,19
                  -122.0832796534698,37.42129328840593,19
                  -122.0832609819207,37.42139213944298,19
                  -122.0829373621737,37.42137236399876,19
                  -122.0829062425667,37.42151569778871,19
                  -122.0828502269665,37.42176282576465,19
                  -122.0829435788635,37.42176776969635,19
                  -122.083217411188,37.42179248552686,19
                  -122.0835970430103,37.4217480074456,19
                  -122.0839455556771,37.42169364237603,19
                  -122.0840077894637,37.42176283815853,19
                  -122.084113587521,37.42174801104392,19
                  -122.0840762473784,37.42171341292375,19
                  -122.0841447047739,37.42167881534569,19
                  -122.084144704223,37.42181720660197,19
                  -122.0842503333074,37.4218170700446,19
                  -122.0844371128284,37.42177253003091,19 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
      &lt;/Folder>
      &lt;Folder>
        &lt;name>Extruded Polygon&lt;/name>
        &lt;description>A simple way to model a building&lt;/description>
        &lt;Placemark>
          &lt;name>The Pentagon&lt;/name>
          &lt;LookAt>
            &lt;longitude>-77.05580139178142&lt;/longitude>
            &lt;latitude>38.870832443487&lt;/latitude>
            &lt;heading>59.88865561738225&lt;/heading>
            &lt;tilt>48.09646074797388&lt;/tilt>
            &lt;range>742.0552506670548&lt;/range>
          &lt;/LookAt>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -77.05788457660967,38.87253259892824,100
                  -77.05465973756702,38.87291016281703,100
                  -77.05315536854791,38.87053267794386,100
                  -77.05552622493516,38.868757801256,100
                  -77.05844056290393,38.86996206506943,100
                  -77.05788457660967,38.87253259892824,100 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
            &lt;innerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -77.05668055019126,38.87154239798456,100
                  -77.05542625960818,38.87167890344077,100
                  -77.05485125901024,38.87076535397792,100
                  -77.05577677433152,38.87008686581446,100
                  -77.05691162017543,38.87054446963351,100
                  -77.05668055019126,38.87154239798456,100 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/innerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
      &lt;/Folder>
      &lt;Folder>
        &lt;name>Absolute and Relative&lt;/name>
        &lt;visibility>0&lt;/visibility>
        &lt;description>Four structures whose roofs meet exactly. Turn on/off
          terrain to see the difference between relative and absolute
          positioning.&lt;/description>
        &lt;LookAt>
          &lt;longitude>-112.3348969157552&lt;/longitude>
          &lt;latitude>36.14845533214919&lt;/latitude>
          &lt;altitude>0&lt;/altitude>
          &lt;heading>-86.91235037566909&lt;/heading>
          &lt;tilt>49.30695423894192&lt;/tilt>
          &lt;range>990.6761201087104&lt;/range>
        &lt;/LookAt>
        &lt;Placemark>
          &lt;name>Absolute&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#transBluePoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;tessellate>1&lt;/tessellate>
            &lt;altitudeMode>absolute&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -112.3372510731295,36.14888505105317,1784
                  -112.3356128688403,36.14781540589019,1784
                  -112.3368169371048,36.14658677734382,1784
                  -112.3384408457543,36.14762778914076,1784
                  -112.3372510731295,36.14888505105317,1784 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
        &lt;Placemark>
          &lt;name>Absolute Extruded&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;styleUrl>#transRedPoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;tessellate>1&lt;/tessellate>
            &lt;altitudeMode>absolute&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -112.3396586818843,36.14637618647505,1784
                  -112.3380597654315,36.14531751871353,1784
                  -112.3368254237788,36.14659596244607,1784
                  -112.3384555043203,36.14762621763982,1784
                  -112.3396586818843,36.14637618647505,1784 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
        &lt;Placemark>
          &lt;name>Relative&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;LookAt>
            &lt;longitude>-112.3350152490417&lt;/longitude>
            &lt;latitude>36.14943123077423&lt;/latitude>
            &lt;altitude>0&lt;/altitude>
            &lt;heading>-118.9214100848499&lt;/heading>
            &lt;tilt>37.92486261093203&lt;/tilt>
            &lt;range>345.5169113679813&lt;/range>
          &lt;/LookAt>
          &lt;styleUrl>#transGreenPoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;tessellate>1&lt;/tessellate>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -112.3349463145932,36.14988705767721,100
                  -112.3354019540677,36.14941108398372,100
                  -112.3344428289146,36.14878490381308,100
                  -112.3331289492913,36.14780840132443,100
                  -112.3317019516947,36.14680755678357,100
                  -112.331131440106,36.1474173426228,100
                  -112.332616324338,36.14845453364654,100
                  -112.3339876620524,36.14926570522069,100
                  -112.3349463145932,36.14988705767721,100 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
        &lt;Placemark>
          &lt;name>Relative Extruded&lt;/name>
          &lt;visibility>0&lt;/visibility>
          &lt;LookAt>
            &lt;longitude>-112.3351587892382&lt;/longitude>
            &lt;latitude>36.14979247129029&lt;/latitude>
            &lt;altitude>0&lt;/altitude>
            &lt;heading>-55.42811560891606&lt;/heading>
            &lt;tilt>56.10280503739589&lt;/tilt>
            &lt;range>401.0997279712519&lt;/range>
          &lt;/LookAt>
          &lt;styleUrl>#transYellowPoly&lt;/styleUrl>
          &lt;Polygon>
            &lt;extrude>1&lt;/extrude>
            &lt;tessellate>1&lt;/tessellate>
            &lt;altitudeMode>relativeToGround&lt;/altitudeMode>
            &lt;outerBoundaryIs>
              &lt;LinearRing>
                &lt;coordinates> -112.3348783983763,36.1514008468736,100
                  -112.3372535345629,36.14888517553886,100
                  -112.3356068927954,36.14781612679284,100
                  -112.3350034807972,36.14846469024177,100
                  -112.3358353861232,36.1489624162954,100
                  -112.3345888301373,36.15026229372507,100
                  -112.3337937856278,36.14978096026463,100
                  -112.3331798208424,36.1504472788618,100
                  -112.3348783983763,36.1514008468736,100 &lt;/coordinates>
              &lt;/LinearRing>
            &lt;/outerBoundaryIs>
          &lt;/Polygon>
        &lt;/Placemark>
      &lt;/Folder>
    &lt;/Folder>
  &lt;/Document>
&lt;/kml>
</pre>



<p>The following is the resulting CSV after running the above code snippet (new CSV file: <code>'my_file.csv'</code>):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">36.10677870477137,-112.0814237830345,36.0905099328766,-112.0870267752693
36.10673460007995,-112.080622229595,36.09049598612422,-112.085242575315
36.09447672602546,-112.265654928602,36.09342608838671,-112.2660384528238,36.09251058776881,-112.2668139013453,36.09189827357996,-112.2677826834445,36.0913137941187,-112.2688557510952,36.0903677207521,-112.2694810717219,36.08932171487285,-112.2695268555611,36.08850916060472,-112.2690144567276,36.08753813597956,-112.2681528815339,36.08682685262568,-112.2670588176031,36.08646312301303,-112.2657374587321
36.07954952145647,-112.2550785337791,36.08117083492122,-112.2549277039738,36.08260761307279,-112.2552505069063,36.08395660588506,-112.2564540158376,36.08511401044813,-112.2580238976449,36.08584355239394,-112.2595218489022,36.08612634548589,-112.2608216347552,36.08626019085147,-112.262073428656,36.08621519860091,-112.2633204928495,36.08627897945274,-112.2644963846444,36.08649599090644,-112.2656969554589
36.09886943729116,-112.2532845153347,36.09919570465255,-112.2540466121145,36.09984998366178,-112.254734666947,36.10051310621746,-112.255493345654,36.10108441943419,-112.2563157098468,36.10159722088088,-112.2568033076439,36.10204323542867,-112.257494011321,36.10229131995655,-112.2584106072308,36.10240001286358,-112.2596588987972,36.10213176873407,-112.2610581199487,36.10157011437219,-112.2626285262793
36.09445214722695,-112.2656634181359,36.09520916122063,-112.2652238941097,36.09580763864907,-112.2645079986395,36.09628572284063,-112.2638827428817,36.09679275951239,-112.2635746835406,36.09740038871899,-112.2635711822407,36.09804913435539,-112.2640296531825,36.09880337400301,-112.264327720538,36.09963644790288,-112.2642436562271,36.10055381117246,-112.2639148687042,36.10149062823369,-112.2626894973474
37.42257124044786,-122.0848938459612,37.42211922626856,-122.0849580979198,37.42207183952619,-122.0847469573047,37.42209006729676,-122.0845725380962,37.42215932700895,-122.0845954886723,37.42227278564371,-122.0838521118269,37.42203539112084,-122.083792243335,37.42209006957106,-122.0835076656616,37.42200987395161,-122.0834709464152,37.4221046494946,-122.0831221085748,37.42226503990386,-122.0829247374572,37.42231242843094,-122.0829339169385,37.42225046087618,-122.0833837359737,37.42234159228745,-122.0833607854248,37.42237075460644,-122.0834204551642,37.42251292011001,-122.083659133885,37.42265873093781,-122.0839758438952,37.42265143972521,-122.0842374743331,37.4226514386435,-122.0845036949503,37.42261133916315,-122.0848020460801,37.42256395055121,-122.0847882750515,37.42257124044786,-122.0848938459612
37.42227033155257,-122.0857412771483,37.42231408832346,-122.0858169768481,37.42230337469744,-122.085852582875,37.42225686138789,-122.0858799945639,37.4222311076138,-122.0858860101409,37.42220250173855,-122.0858069157288,37.42214027058678,-122.0858379542653,37.42208690214408,-122.0856732640519,37.42214885429042,-122.0856022926407,37.422128290487,-122.0855902778436,37.42208171967246,-122.0855841672237,37.42210455874995,-122.0854852065741,37.42214267949824,-122.0855067264352,37.42212783846172,-122.0854430712915,37.42251282407603,-122.0850990714904,37.42281815323651,-122.0856769818632,37.42244918858722,-122.0860162273783,37.42229239604253,-122.0857260327004,37.42227033155257,-122.0857412771483
37.42136208886969,-122.0857862287242,37.42136935989481,-122.0857312990603,37.42140934910903,-122.0857312992918,37.42138390166565,-122.0856077073679,37.42137299550869,-122.0855802426516,37.42137299504316,-122.0852186221971,37.42161656508265,-122.0852277765639,37.42160565894403,-122.0852598189347,37.42168200156,-122.0852598185499,37.42170017860346,-122.0852369311478,37.42176197982575,-122.0852643957828,37.42176198013907,-122.0853239032746,37.421852864452,-122.0853559454324,37.42188921823734,-122.0854108752463,37.42189285337048,-122.0854795379357,37.42188921797546,-122.0855436229819,37.42186013499926,-122.0856260178042,37.42186013453605,-122.085937287963,37.42160898590042,-122.0859428718666,37.42157992759144,-122.0859655469861,37.42147115002957,-122.0858640462341,37.42140571326184,-122.0858548911215,37.4214057134039,-122.0858091162768,37.42136208886969,-122.0857862287242
37.42177253003091,-122.0844371128284,37.42191111542896,-122.0845118855746,37.42178755121535,-122.0850470999805,37.42143663023161,-122.0850719913391,37.42137237822116,-122.084916406232,37.42137237801626,-122.0842193868167,37.42147617161496,-122.08421938659,37.4214613409357,-122.0838086419991,37.42131306410796,-122.0837899728564,37.42129328840593,-122.0832796534698,37.42139213944298,-122.0832609819207,37.42137236399876,-122.0829373621737,37.42151569778871,-122.0829062425667,37.42176282576465,-122.0828502269665,37.42176776969635,-122.0829435788635,37.42179248552686,-122.083217411188,37.4217480074456,-122.0835970430103,37.42169364237603,-122.0839455556771,37.42176283815853,-122.0840077894637,37.42174801104392,-122.084113587521,37.42171341292375,-122.0840762473784,37.42167881534569,-122.0841447047739,37.42181720660197,-122.084144704223,37.4218170700446,-122.0842503333074,37.42177253003091,-122.0844371128284
38.87253259892824,-77.05788457660967,38.87291016281703,-77.05465973756702,38.87053267794386,-77.05315536854791,38.868757801256,-77.05552622493516,38.86996206506943,-77.05844056290393,38.87253259892824,-77.05788457660967
38.87154239798456,-77.05668055019126,38.87167890344077,-77.05542625960818,38.87076535397792,-77.05485125901024,38.87008686581446,-77.05577677433152,38.87054446963351,-77.05691162017543,38.87154239798456,-77.05668055019126
36.14888505105317,-112.3372510731295,36.14781540589019,-112.3356128688403,36.14658677734382,-112.3368169371048,36.14762778914076,-112.3384408457543,36.14888505105317,-112.3372510731295
36.14637618647505,-112.3396586818843,36.14531751871353,-112.3380597654315,36.14659596244607,-112.3368254237788,36.14762621763982,-112.3384555043203,36.14637618647505,-112.3396586818843
36.14988705767721,-112.3349463145932,36.14941108398372,-112.3354019540677,36.14878490381308,-112.3344428289146,36.14780840132443,-112.3331289492913,36.14680755678357,-112.3317019516947,36.1474173426228,-112.331131440106,36.14845453364654,-112.332616324338,36.14926570522069,-112.3339876620524,36.14988705767721,-112.3349463145932
36.1514008468736,-112.3348783983763,36.14888517553886,-112.3372535345629,36.14781612679284,-112.3356068927954,36.14846469024177,-112.3350034807972,36.1489624162954,-112.3358353861232,36.15026229372507,-112.3345888301373,36.14978096026463,-112.3337937856278,36.1504472788618,-112.3331798208424,36.1514008468736,-112.3348783983763
</pre>



<h2 class="wp-block-heading">How to Convert KMZ to CSV in Python?</h2>



<p>Files in the KML format are often packaged and distributed as KMZ files with the suffix <code>.kmz</code>. </p>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2139.png" alt="ℹ" class="wp-smiley" style="height: 1em; max-height: 1em;" /> KMZ files are zipped KML files with a special format for the content: a single root KML document named <code>doc.kml</code>. It has all additional files such as images and icons and 3D models located in the zip folder as well.</p>



<p class="has-global-color-8-background-color has-background">To convert a KMZ file to a CSV, you can unzip it and convert the root KML file to a <code>.csv</code> file in Python by using the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-beautifulsoup-xml-to-dict-json-dataframe-csv/" data-type="post" data-id="474965" target="_blank">BeautifulSoup</a> and the <code>csv</code> libraries. You use the former to read the XML-structured KML file and the latter to write the CSV file row by row. </p>



<p>The remaining (non-CSV) contents of the zip folder, such as images, can hardly be converted to a CSV anyways.</p>



<p>See the code above for the KML to CSV conversion.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>
<p>The post <a href="https://blog.finxter.com/python-how-to-convert-kml-to-csv/">Python &#8211; How to Convert KML to CSV?</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Python BeautifulSoup XML to Dict, JSON, DataFrame, CSV</title>
		<link>https://blog.finxter.com/python-beautifulsoup-xml-to-dict-json-dataframe-csv/</link>
		
		<dc:creator><![CDATA[Jordan Marshall]]></dc:creator>
		<pubDate>Sat, 16 Jul 2022 15:04:30 +0000</pubDate>
				<category><![CDATA[BeautifulSoup]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Web Scraping]]></category>
		<category><![CDATA[XML]]></category>
		<guid isPermaLink="false">https://blog.finxter.com/?p=474965</guid>

					<description><![CDATA[<p>Though Python’s BeautifulSoup module was designed to scrape HTML files, it can also be used to parse XML files. In today’s professional marketplace, it is useful to be able to change an XML file into other formats, specifically dictionaries, CSV, JSON, and dataframes according to specific needs. In this article, we will discuss that process. ... <a title="Python BeautifulSoup XML to Dict, JSON, DataFrame, CSV" class="read-more" href="https://blog.finxter.com/python-beautifulsoup-xml-to-dict-json-dataframe-csv/" aria-label="Read more about Python BeautifulSoup XML to Dict, JSON, DataFrame, CSV">Read more</a></p>
<p>The post <a href="https://blog.finxter.com/python-beautifulsoup-xml-to-dict-json-dataframe-csv/">Python BeautifulSoup XML to Dict, JSON, DataFrame, CSV</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p><em>Though Python’s BeautifulSoup module was designed to scrape HTML files, it can also be used to parse XML files. </em></p>



<p><em>In today’s professional marketplace, it is useful to be able to change an XML file into other formats, specifically dictionaries, CSV, JSON, and dataframes according to specific needs. </em></p>



<p><em>In this article, we will discuss that process.</em></p>



<h2 class="wp-block-heading">Scraping XML with BeautifulSoup</h2>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Extensible Markup Language</strong> or <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/xml-developer-income-and-opportunity/" data-type="post" data-id="242005" target="_blank">XML</a></strong> differs from <a rel="noreferrer noopener" href="https://blog.finxter.com/html-developer-income-and-opportunity/" data-type="post" data-id="191232" target="_blank">HTML</a> in that HTML primarily deals with how information is displayed on a webpage, and XML handles how data is stored and transmitted. XML also uses custom tags and is designed to be user and machine-readable. </p>



<p>When inspecting a webpage, a statement at the top of the page will denote what type of file you are viewing. </p>



<p>For an XML file, you may see <code>&lt;?xml version="1.0"?&gt;</code>. </p>



<p class="has-base-background-color has-background">As a side note, “<code>version 1.0</code>” is a little deceiving in that several modifications have been made since its inception in 1998 the name has just not changed. </p>



<p>Despite the differences between HTML and XML, because <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-beautifulsoup-on-pycharm/" data-type="URL" data-id="https://blog.finxter.com/how-to-install-beautifulsoup-on-pycharm/" target="_blank">BeautifulSoup</a> creates a <strong>Python object tree</strong>, it can be used to parse both. The process for parsing both is similar. For this article, I will be using a sample XML file from <a rel="noreferrer noopener" href="https://www.w3schools.com/xml/cd_catalog.xml" data-type="URL" data-id="https://www.w3schools.com/xml/cd_catalog.xml" target="_blank">w3 schools.com</a>.</p>



<p>Import the BeautifulSoup library and requests modules to scrape this file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Import needed libraries
from pprint import pprint
from bs4 import BeautifulSoup
import requests</pre>



<p>Once these have been imported, request the content of the webpage.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Request data
webpage = requests.get("https://www.w3schools.com/xml/cd_catalog.xml")
data = webpage.content
pprint(data)</pre>



<p>At this point, I like to print just to make sure I am getting what I need. I use the <code>pprint()</code> function to make it more readable.</p>



<p>Next, create a BeautifulSoup object and declare the parser to be used. Because it is an XML file, use an XML parser.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Create a BeautifulSoup object
soup = BeautifulSoup(data, 'xml')
print(soup.prettify())</pre>



<p>With that printed, you can see the object tree created by BeautifulSoup. The parent, “<code>&lt;CATALOG&gt;</code>”, its child “<code>&lt;CD&gt;</code>”, and all of the children of “<code>CD</code>” are displayed.</p>



<p><strong>Output of the first CD:</strong></p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;CATALOG>
&lt;CD>
&lt;TITLE>Empire Burlesque&lt;/TITLE>
&lt;ARTIST>Bob Dylan&lt;/ARTIST>
&lt;COUNTRY>USA&lt;/COUNTRY>
&lt;COMPANY>Columbia&lt;/COMPANY>
&lt;PRICE>10.90&lt;/PRICE>
&lt;YEAR>1985&lt;/YEAR>
&lt;/CD></pre>



<p>All left is to scrape the desired data and display it. </p>



<p>Using the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-enumerate/" data-type="post" data-id="20466" target="_blank"><code>enumerate()</code></a> and <code><a rel="noreferrer noopener" href="https://blog.finxter.com/parsing-xml-using-beautifulsoup-in-python/" data-type="post" data-id="17772" target="_blank">find_all()</a></code> function each occurrence of a tag can be found, and its contents can be placed into a <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">list</a>. </p>



<p>After that, using a <code>for</code> loop, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-unpacking/" data-type="post" data-id="396420" target="_blank">unpack</a> the created lists, and create groupings. The <code>.text</code> attribute string and <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-strip/" data-type="post" data-id="26104" target="_blank">strip()</a></code> function gives only the text and <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-remove-extra-whitespaces-in-beautifulsoup/" data-type="post" data-id="223870" target="_blank">removes the white space</a>. </p>



<p>Just for readability, <a href="https://blog.finxter.com/how-to-skip-a-line-in-python-using-n/" data-type="post" data-id="451007" target="_blank" rel="noreferrer noopener">print a blank line</a> after each grouping.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Scrape data
parent = soup.find('CATALOG')
for n, tag in enumerate(parent.find_all('CD')):
    title = [x for x in tag.find_all('TITLE')]
    artist = [x for x in tag.find_all('ARTIST')]
    country = [x for x in tag.find_all('COUNTRY')]
    company = [x for x in tag.find_all('COMPANY')]
    price = [x for x in tag.find_all('PRICE')]
    year = [x for x in tag.find_all('YEAR')]
    # view data
    for item in title:
        print('Title: ', item.text.strip())
    for item in artist:
        print('Artist: ', item.text.strip())
    for item in country:
        print('Country: ', item.text.strip())
    for item in company:
        print('Company: ', item.text.strip())
    for item in price:
        print('Price: ', item.text.strip())
    for item in year:
        print('Year: ', item.text.strip())
    print()</pre>



<p>With that, the CDs should be cataloged in this format.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Title:  Empire Burlesque
Artist:  Bob Dylan
Country:  USA
Company:  Columbia
Price:  10.90
Year:  1985 </pre>



<h2 class="wp-block-heading">XML to Dictionary</h2>



<p>Besides lists, <a href="https://blog.finxter.com/python-dictionary/" data-type="post" data-id="5232" target="_blank" rel="noreferrer noopener">dictionaries</a> are a common structure for storing data in Python. </p>



<p>Information is stored in key: value pairs. Those pairs are stored within curly <code>{}</code> brackets. </p>



<p class="has-base-background-color has-background"><strong>Example</strong>: <code>capital = {Pennsylvania: Harrisburg, Michigan: Lansing}</code></p>



<p>The key of the pair is case-sensitive and unique. The value can be any data type and may be duplicated. </p>



<p>Accessing the value of the pair can be done via the Key. Since the key cannot be duplicated, finding a value in a large dictionary is easy so long as you know the key. A key list can be obtained using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-keys-method/" data-type="post" data-id="37711" target="_blank">keys()</a></code> method. </p>



<p class="has-base-background-color has-background"><strong>Example</strong>: <code>print(capital.keys())</code></p>



<p>Finding information in a dictionary is quick since you only search for a specific key. </p>



<p>Dictionaries are used quite often, if memory usage is not a concern, because of the quick access. For this reason, it is important to know how to convert information gained in an XML file to a dictionary. </p>



<p class="has-global-color-8-background-color has-background">There are six basic steps to convert an XML to a dictionary:</p>



<ol class="has-global-color-8-background-color has-background wp-block-list"><li><code>import xmltodict</code></li><li><code>import pprint</code></li><li><code>with open('C:\Users\Jordan Marshall\Downloads\cd_catalog.xml', 'r', encoding='utf-8') as file:</code><ul><li><code>cd_xml = file.read()</code></li></ul></li><li><code>cd_dict = xmltodict.parse(cd_xml)</code></li><li><code>cd_dict_list = [dict(x) for x in cd_dict['CATALOG']['CD']]</code></li><li><code>pprint.pprint(cd_dict_list)</code></li></ol>



<p>First, for the conversion, Python has a built-in called <code><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-xmltodict-in-python/" data-type="post" data-id="457149" target="_blank">xmltodict</a></code>. So first import that module and any other modules to be used.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import xmltodict
import pprint</pre>



<p>Second, the file needs to be opened, read, and assigned to a variable.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('C:\\Users\\Jordan Marshall\\Downloads\\cd_catalog.xml', 'r', encoding='utf-8') as file:
    cd_xml = file.read()</pre>



<p>Third, using <code>xmltodict.parse()</code> convert the XML file to a dictionary and view it.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">cd_dict = xmltodict.parse(cd_xml)
cd_dict_list = [dict(x) for x in cd_dict['CATALOG']['CD']]
pprint.pprint(cd_dict_list)</pre>



<p>The output of this is a nice clean <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-create-a-list-of-dictionaries-in-python/" data-type="post" data-id="10576" target="_blank">list of dictionaries</a>. To view all artists, a simple <code>for</code> loop can be used.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for item in cd_dict_list:
    print(item['ARTIST'])</pre>



<h2 class="wp-block-heading">XML to JSON</h2>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>JSON</strong> stands for <strong>JavaScript Object Notation</strong>. These files store data in <code>key:value</code> form like a Python dictionary. JSON files are used primarily to transmit data between web applications and servers. </p>



<p>Converting an XML file to a JSON file requires only a few lines of code.&nbsp;</p>



<p>As always, import the needed libraries and modules.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import json
from pprint import pprint
import xmltodict</pre>



<p>Again, you will see the use of <code>xmltodict</code>. Because of their similarities, first, convert the file to a dictionary and then later write it to a JSON file. The <code>json_dumps()</code> function is used to take in the XML data. That data will later be written to a JSON file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">with open('C:\\Users\\Jordan Marshall\\Downloads\\cd_catalog example.xml') as xml_file:
    data_dict = xmltodict.parse(xml_file.read())
    xml_file.close()
    json_data = json.dumps(data_dict)
    with open('data.json', 'w') as json_file:
        json_file.write(json_data)
        json_file.close()</pre>



<p><strong>Output</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">('{"CATALOG": {"CD": [{"TITLE": "Empire Burlesque", "ARTIST": "Bob Dylan", '
 '"COUNTRY": "USA", "COMPANY": "Columbia", "PRICE": "10.90", "YEAR": "1985"}, '
 '{"TITLE": "Hide your heart", "ARTIST": "Bonnie Tyler", "COUNTRY": "UK", '
 '"COMPANY": "CBS Records", "PRICE": "9.90", "YEAR": "1988"}, {"TITLE": '
 '"Greatest Hits", "ARTIST": "Dolly Parton", "COUNTRY": "USA", "COMPANY": '
 '"RCA", "PRICE": "9.90", "YEAR": "1982"}, {"TITLE": "Still got the blues", '….)
</pre>



<p>The data that started as an XML file has now been written to a JSON file called <code>json_data</code>.&nbsp;</p>



<h2 class="wp-block-heading">XML to DataFrame</h2>



<p>There are a couple of ways to achieve this goal. </p>



<p>Using Python’s <code>ElementTree</code> is one. I am, however, partial to <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="post" data-id="16511" target="_blank">Pandas</a>. </p>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Pandas</strong> is a great module for working with data, and it simplifies many daily tasks of a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-developer-income-and-opportunity/" data-type="post" data-id="189354" target="_blank">programmer</a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/data-scientist-income-and-opportunity/" data-type="post" data-id="332478" target="_blank">data scientist</a>. I strongly suggest <a href="https://blog.finxter.com/pandas-cheat-sheets/" data-type="post" data-id="7977" target="_blank" rel="noreferrer noopener">becoming familiar</a> with this module. </p>



<p>For this code, use a combination of BeautifulSoup and Pandas.</p>



<p>Import the necessary libraries.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd
from bs4 import BeautifulSoup</pre>



<p>To display the output fully, display values may need to be altered. I am going to set the max number of columns as well as the display width. This will overwrite any default settings that may be in place. </p>



<p>Without doing this, you may find some of your columns are replaced by ‘<code>…</code>’ or the columns may be displayed under your first couple of columns.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># set max columns and display width
pd.set_option("display.max_columns", 10)
pd.set_option("display.width", 1000)</pre>



<p>The width and columns can be changed according to your needs. With that completed, <a href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank" rel="noreferrer noopener">open</a> and read the XML file. Store the contents in a variable.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">xml_file = open('C:\\Users\\Jordan Marshall\\Downloads\\cd_catalog.xml', 'r')
contents = xml_file.read()</pre>



<p>Next, create a BeautifulSoup object.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># BeautifulSoup object
soup = BeautifulSoup(contents, 'xml')</pre>



<p>The next step is to extract the data and assign it to a variable.&nbsp;</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Extract data and assign it to a variable
title = soup.find_all("TITLE")
artist = soup.find_all("ARTIST")
country = soup.find_all("COUNTRY")
company = soup.find_all("COMPANY")
price = soup.find_all("PRICE")
year = soup.find_all("YEAR")</pre>



<p>Now a <code>for</code> loop can be used to extract the text. </p>



<p>Should data be added or removed at any time using the length of one of the variables removes the need to know from memory how many items are cataloged. </p>



<p>Place the text in an <a href="https://blog.finxter.com/how-to-create-an-empty-list-in-python/" data-type="post" data-id="453870" target="_blank" rel="noreferrer noopener">empty list</a>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Text
cd_info = []
for i in range(0, len(title)):
    rows = [title[i].get_text(),
            artist[i].get_text(),
            country[i].get_text(),
            company[i].get_text(),
            price[i].get_text(),
            year[i].get_text()]
    cd_info.append(rows)</pre>



<p>Lastly, create the data frame and name the columns.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># Create a dataframe with Pandas and print
df = pd.DataFrame(cd_info, columns=['Title', 'Artist ', '   Company', 'Country', '   Price', '   Year'])
print(df)</pre>



<p><strong>Output</strong></p>



<pre class="wp-block-preformatted"><code>            Title                  Artist              Country         Company      Price     Year
0           Empire Burlesque       Bob Dylan           USA             Columbia     10.90     1985
1           Hide your heart        Bonnie Tyler        UK              CBS Records  9.90      1988
2           Greatest Hits          Dolly Parton        USA             RCA          9.90      1982</code></pre>



<p>A nice, neat table containing each CD’s data has been created.</p>



<h2 class="wp-block-heading">XML to CSV</h2>



<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> A CSV file or comma-separated values file contains plain text easily readable by the user. It can contain numbers and letters only and is used to exchange data between apps. CSV files can be opened by any editor. </p>



<p>For example, Microsoft Excel. Each line represents a new row of data. The comma represents a new column. Using the code from above the XML file can be converted to a CSV file with one new line.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">catalog = df.to_csv('cd catalog.csv')</pre>



<p>With that, go to <code>files</code> and search the <code>C:</code> drive for <code>'cd catalog.csv'</code>. It will open in the default program used for spreadsheets. In this case Microsoft Excel.</p>



<figure class="wp-block-table is-style-stripes"><table><tbody><tr><td>Title</td><td>Artist&nbsp;</td><td>Country</td><td>Company</td><td>Price</td><td>&nbsp;Year</td></tr><tr><td>Empire Burlesque</td><td>Bob Dylan</td><td>USA</td><td>Columbia</td><td>10.90</td><td>1985</td></tr><tr><td>Hide your heart</td><td>Bonnie Tyler</td><td>UK</td><td>CBS Records</td><td>9.90</td><td>1988</td></tr><tr><td>Greatest Hits</td><td>Dolly Parton</td><td>USA</td><td>RCA</td><td>9.90</td><td>1982</td></tr><tr><td>Still got the blues</td><td>Gary Moore</td><td>UK</td><td>Virgin records</td><td>10.20</td><td>1990</td></tr><tr><td>Eros</td><td>Eros Ramazzotti</td><td>EU</td><td>BMG</td><td>9.90</td><td>1997</td></tr><tr><td>One night only</td><td>Bee Gees</td><td>UK</td><td>Polydor</td><td>10.90</td><td>1998</td></tr><tr><td>Sylvias Mother</td><td>Dr.Hook</td><td>UK</td><td>CBS</td><td>8.10</td><td>1973</td></tr><tr><td>Maggie May</td><td>Rod Stewart</td><td>UK</td><td>Pickwick</td><td>8.50</td><td>1990</td></tr><tr><td>Romanza</td><td>Andrea Bocelli</td><td>EU</td><td>Polydor</td><td>10.80</td><td>1996</td></tr></tbody></table></figure>



<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f30d.png" alt="🌍" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Tutorial</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/python-how-to-convert-kml-to-csv/" data-type="URL" data-id="https://blog.finxter.com/python-how-to-convert-kml-to-csv/" target="_blank">How to Convert a KML to a CSV File in Python?</a></p>
<p>The post <a href="https://blog.finxter.com/python-beautifulsoup-xml-to-dict-json-dataframe-csv/">Python BeautifulSoup XML to Dict, JSON, DataFrame, CSV</a> appeared first on <a href="https://blog.finxter.com">Be on the Right Side of Change</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Minified using Disk

Served from: blog.finxter.com @ 2026-06-26 10:18:40 by W3 Total Cache
-->