Element Tree find output empty text
<p>I have a problem using Element Tree to extract the text.</p>

<p>My format of my xml file is</p>

<pre><code>&lt;elecs id = 'elecs'&gt;
    &lt;elec id = "CLM-0001" num = "0001"&gt;
        &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;
        &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;
    &lt;/elec&gt;
    &lt;elec id = "CLM-0002" num = "0002"&gt;
         &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;
         &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;
    &lt;/elec&gt;
 &lt;/elecs&gt;
</code></pre>

<p>I want to extract out all the text inside the tag </p>

<p>Assume that our xml file is in the variable xml</p>

<pre><code>import xml.etree.ElementTree as ET
import lxml import etree
parser = etree.XMLParser(recover = True)
contents = open(xml).read()
tree = ET.fromstring(contents, parser = parser)
elecsN = tree.find('elecs')
for element in elecsN:
    print element.text
</code></pre>

<p>The problem is, the code above returns empty strings. I have tried my code above with other tags in my document and it works. I do not know why it returns empty string this time.</p>

<p>Is there anyway i can solve this problem.</p>

<p>Thank you very much</p>

<p>If you actually mean 'any way' you could use lxml.</p>

<pre><code>&gt;&gt;&gt; from io import StringIO
&gt;&gt;&gt; html = StringIO('''\
... &lt;elecs id = 'elecs'&gt;
...     &lt;elec id = "CLM-0001" num = "0001"&gt;
...             &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;
...             &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;            
...     &lt;/elec&gt;
...     &lt;elec id = "CLM-0002" num = "0002"&gt;    
...          &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;
...          &lt;elec-text&gt; blah blah blah &lt;/elec-text&gt;         
...     &lt;/elec&gt;
... &lt;/elecs&gt;
... '''
... )
&gt;&gt;&gt; from lxml import etree
&gt;&gt;&gt; doc = etree.parse(html)
&gt;&gt;&gt; doc.xpath('//elecs/elec/*/text()')
[' blah blah blah ', ' blah blah blah ', ' blah blah blah ', ' blah blah blah ']
</code></pre>

python xml elementtree