python XML get text inside <p>...</p> tag
<p>I guys, I have an xml structure which looks somewhat like this.</p>

<pre><code>&lt;abstract&gt;
  &lt;p id = "p-0001" num = "0000"&gt;
     blah blah blah
  &lt;/p&gt;
&lt;/abstract&gt;
</code></pre>

<p>I would like to extract the <code>&lt;p&gt;</code> tag inside the <code>&lt;abstract&gt;</code> tag only.</p>

<p>I tried:</p>

<pre><code>import xml.etree.ElementTree as ET

xroot = ET.parse('100/A/US07640598-20100105.XML').getroot()

for row in xroot.iter('p'):
     print row.text
</code></pre>

<p>This get all the <code>&lt;p&gt;</code> tag in my xml which is not a good idea.</p>

<p>Is there anyway i can extract the text inside </p>

<p>My desire output would be extracting "blah blah blah"</p>

<p>You can use an <em>XPath expression</em> to search for <code>p</code> elements specifically inside the <code>abstract</code>:</p>

<pre><code>for p in xroot.xpath(".//abstract//p"):
    print(p.text.strip())
</code></pre>

<p>Or, if using <code>iter()</code> you may have a nested loop:</p>

<pre><code>for abstract in xroot.iter('abstract'):
    for p in abstract.iter('p'):
        print(p.text.strip())
</code></pre>

python xml