<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: python &amp;amp; xtree: extracting information from xml in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265895#M20460</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;1. Your example XML is invalid (I assume that's just an error snipping out the example, you have an unclosed "sourceAttributes" element which it looks like you're trying to close with an "imageAttributes" element and you have an open "&amp;lt;" in there as well.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Your XPath expression will only search the first level. Use ".//" to recursively search.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3. Your xml declares a namespace, you need to use it. i.e.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;xmlns='{http://www.rsi.ca/rs2/prod/xml/schemas}'
for path in [ './/%ssatellite', './/%sbeamModeMnemonic', './/%srawDataStartTime' ]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; node = tree.find(path%xmlns)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print path, node
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:03:31 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2021-12-11T13:03:31Z</dc:date>
    <item>
      <title>python &amp;amp;amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265893#M20458</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to extract the associated text with the following xml tags: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;satellite&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;beamModeMnemonic&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;rawDataStartTime&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For now I am simply trying to print the value but eventually I would like to 'stuff' this text into variable for geoprocessing with arcpy. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Below is my attempt at coding this but obviously not working. I followed a simple example from this link (Sections: Traversing the Parsed Tree &amp;amp; Parsed Node Attributes):&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://blog.doughellmann.com/2010/03/pymotw-parsing-xml-documents-with.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions on how to do this simple task?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;---------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
from xml.etree import ElementTree
import arcpy, string, os

arcpy.env.worspace='C:/Alice/scripting/script'

with open('product.xml', 'rt') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tree = ElementTree.parse(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
#this prints all the tags and attributes, needed to know if it was reading file=yes
for node in tree.getiterator():
&amp;nbsp;&amp;nbsp;&amp;nbsp; print node.tag, node.attrib&amp;nbsp;&amp;nbsp;&amp;nbsp; 

#this give an error!!!
for path in [ './satellite', './beamModeMnemonic', './rawDataStartTime' ]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; node = tree.find(path)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp; node text:', node.tag
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp; node text:', node.text
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="color:&amp;quot;Red&amp;quot;;"&gt;The errors I get in PythonWIn are: &lt;BR /&gt;"print node.tag &lt;BR /&gt;AttributeError: 'NoneType' object has no attribute 'tag'" and &lt;BR /&gt;"print node.text &lt;BR /&gt;AttributeError: 'NoneType' object has no attribute 'text'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Snippet of xml file showing tags tags required:&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;---------------------------------------------------------------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&amp;gt;
&amp;lt;product xmlns="http://www.rsi.ca/rs2/prod/xml/schemas" copyright="RADARSAT-2 Data and Products (c) MacDonald, Dettwiler and Associates Ltd., 2011 - All Rights Reserved." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.rsi.ca/rs2/prod/xml/schemas schemas/rs2prod_product.xsd"&amp;gt;
 &amp;lt;productId&amp;gt;PDS_01583330&amp;lt;/productId&amp;gt;
 &amp;lt;documentIdentifier&amp;gt;RN-RP-51-2713, Issue 1/8&amp;lt;/documentIdentifier&amp;gt;
 &amp;lt;sourceAttributes&amp;gt;
&amp;nbsp; &amp;lt;satellite&amp;gt;RADARSAT-2&amp;lt;/satellite&amp;gt;
&amp;nbsp; &amp;lt;sensor&amp;gt;SAR&amp;lt;/sensor&amp;gt;
&amp;nbsp; &amp;lt;inputDatasetId&amp;gt;/Fred/rsat2/166026P&amp;lt;/inputDatasetId&amp;gt;
&amp;nbsp; &amp;lt;imageId&amp;gt;128973&amp;lt;/imageId&amp;gt;
&amp;nbsp; &amp;lt;inputDatasetFacilityId&amp;gt;Not Specified&amp;lt;/inputDatasetFacilityId&amp;gt;
&amp;nbsp; &amp;lt;beamModeId&amp;gt;110&amp;lt;/beamModeId&amp;gt;
&amp;nbsp; &amp;lt;beamModeMnemonic&amp;gt;S6&amp;lt;/beamModeMnemonic&amp;gt;
&amp;nbsp; &amp;lt;rawDataStartTime&amp;gt;2011-04-18T12:32:56.257634Z&amp;lt;/rawDataStartTime&amp;gt;
&amp;nbsp; &amp;lt;
&amp;nbsp; &amp;lt;fullResolutionImageData pole="HV"&amp;gt;imagery_HV.tif&amp;lt;/fullResolutionImageData&amp;gt;
 &amp;lt;/imageAttributes&amp;gt;
&amp;lt;/product&amp;gt;

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:03:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265893#M20458</guid>
      <dc:creator>AliceDeschamps1</dc:creator>
      <dc:date>2021-12-11T13:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: python &amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265894#M20459</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You would be best to ask the guy who made the example in the link; this has nothing to do with ArcGIS &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, just from looking at your xml and the similar example on his page I can see that your xml has a deeper level (? sorry, I don't know much about xml or the terminology). The error that Python is giving shows that &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;node = tree.find(path)&lt;/SPAN&gt;&lt;SPAN&gt; is returning &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;None&lt;/SPAN&gt;&lt;SPAN&gt;, probably indicating the path wasn't found (at that level...?). When you query the node, which is None, for .tag and .text it fails as None doesn't contain any information.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could try looking through more of his examples, or ask the guy, how do examine deeper levels of the xml.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jul 2011 04:57:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265894#M20459</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2011-07-12T04:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: python &amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265895#M20460</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;1. Your example XML is invalid (I assume that's just an error snipping out the example, you have an unclosed "sourceAttributes" element which it looks like you're trying to close with an "imageAttributes" element and you have an open "&amp;lt;" in there as well.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Your XPath expression will only search the first level. Use ".//" to recursively search.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3. Your xml declares a namespace, you need to use it. i.e.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;xmlns='{http://www.rsi.ca/rs2/prod/xml/schemas}'
for path in [ './/%ssatellite', './/%sbeamModeMnemonic', './/%srawDataStartTime' ]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; node = tree.find(path%xmlns)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print path, node
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:03:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265895#M20460</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T13:03:31Z</dc:date>
    </item>
    <item>
      <title>Re: python &amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265896#M20461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The snippet of xml is a subset showing the first 17 lines only (entire file = 2000lines). &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I need to extract the text from specific tag for vector attribution.&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I need a simple example to get me going.&amp;nbsp; This link is a good GIS example but it's a bit more complicated than my current skill level, but I will persist.....&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2010/05/07/Handling-XML-with-Python-in-ArcGIS.aspx"&gt;http://blogs.esri.com/Dev/blogs/geoprocessing/archive/2010/05/07/Handling-XML-with-Python-in-ArcGIS.aspx&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jul 2011 11:06:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265896#M20461</guid>
      <dc:creator>AliceDeschamps1</dc:creator>
      <dc:date>2011-07-12T11:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: python &amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265897#M20462</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Have you tried to use the code snippet from my previous post?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;xmlns='{http://www.rsi.ca/rs2/prod/xml/schemas}'
for path in [ './/%ssatellite', './/%sbeamModeMnemonic', './/%srawDataStartTime' ]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; node = tree.find(path%xmlns)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'node.text:', node.text
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'node.tag:', node.tag
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;This prints:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
node.text: RADARSAT-2
node.tag: {http://www.rsi.ca/rs2/prod/xml/schemas}satellite
node.text: S6
node.tag: {http://www.rsi.ca/rs2/prod/xml/schemas}beamModeMnemonic
node.text: 2011-04-18T12:32:56.257634Z
node.tag: {http://www.rsi.ca/rs2/prod/xml/schemas}rawDataStartTime
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:03:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265897#M20462</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T13:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: python &amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265898#M20463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Luke.&amp;nbsp;&amp;nbsp; Yes your code snippet spits out the 3 text values that I need.&amp;nbsp; Below is a modified version. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So is this correct:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1) xmlns is the root?&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2) I need to specify the tag + root to get the text&amp;nbsp; like in this ('.//%ssatellite'%xmlns).&amp;nbsp;&amp;nbsp; The .// level of tag nesting.&amp;nbsp; %s converts to string.&amp;nbsp; How does it concatenate the two?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from xml.etree import ElementTree
import arcpy, string, os
arcpy.env.overwriteOutput = True

arcpy.env.worspace='C:/scripting/script'

with open('product.xml', 'rt') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tree = ElementTree.parse(f)
root = tree.getroot()
print root

xmlns='{http://www.rsi.ca/rs2/prod/xml/schemas}'

item1=tree.find('.//%ssatellite'%xmlns).text
item2=tree.find('.//%sbeamModeMnemonic'%xmlns).text
item3=tree.find('.//%srawDataStartTime'%xmlns).text
print item1
print item2
print item3&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This also works and spits out the same values. I find has simpler syntax but I needed your example to get it working!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from xml.etree import ElementTree
import arcpy, string, os
arcpy.env.overwriteOutput = True

arcpy.env.worspace='C:/scripting/script'

with open('product.xml', 'rt') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tree = ElementTree.parse(f)

root = tree.getroot()
print root
rootText='.//{http://www.rsi.ca/rs2/prod/xml/schemas}'

satellite=tree.find(rootText + 'satellite').text
beamMode=tree.find(rootText + 'beamModeMnemonic').text
timeUTC=tree.find(rootText + 'rawDataStartTime').text
print satellite
print beamMode
print timeUTC
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now let's see if I can manage to do something useful with the info.....&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:03:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265898#M20463</guid>
      <dc:creator>AliceDeschamps1</dc:creator>
      <dc:date>2021-12-11T13:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: python &amp; xtree: extracting information from xml</title>
      <link>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265899#M20464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;So is this correct:&lt;BR /&gt;1) xmlns is the root?&amp;nbsp; &lt;BR /&gt;2) I need to specify the tag + root to get the text&amp;nbsp; like in this ('.//%ssatellite'%xmlns).&amp;nbsp;&amp;nbsp; The .// level of tag nesting.&amp;nbsp; %s converts to string.&amp;nbsp; How does it concatenate the two?&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. No. xmlns is the namespace. The root is {namespace}product&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. If your XML specifies a namespace, you need to use {namespace}tag not just tag. % is a string operator, it is used to replace string format codes (e.g. %s) with one or more values. See &lt;/SPAN&gt;&lt;A href="http://docs.python.org/library/stdtypes.html#string-formatting"&gt;http://docs.python.org/library/stdtypes.html#string-formatting&lt;/A&gt;&lt;SPAN&gt; for more info.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jul 2011 22:00:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-amp-amp-amp-xtree-extracting-information/m-p/265899#M20464</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2011-07-13T22:00:28Z</dc:date>
    </item>
  </channel>
</rss>

