Parse an XML file to get a full tag by using Python's lxml package
<p>I've got the following XML file:</p>

<pre><code>&lt;root&gt;

    &lt;scene name="scene1"&gt;
        &lt;view ath="0" atv="10"/&gt;
        &lt;image url="img1.jgp"/&gt;
        &lt;hotspot name="hot1"/&gt;
    &lt;/scene&gt;

    &lt;scene name="scene2"&gt;
        &lt;view ath="20" atv="10"/&gt;
        &lt;image url="img2.jgp"/&gt;
        &lt;hotspot name="hot2"/&gt;
    &lt;/scene&gt;

&lt;/root&gt;
</code></pre>

<p>I'm writing a Python script using lxml package, to get the entire <code>view</code> tag within <code>scene1</code>. That is:</p>

<pre><code>&lt;view ath="0" atv="10" /&gt;
</code></pre>

<p>I've read the lxml documentation but all I can find is how to get the tag, its attributes or its content, but not the entire tag.</p>

<p>Can anybody at least point me in the right direction? Does lxml has a function or a method to achieve this?</p>

<p>Thanks,</p>

<p>Rafael </p>

<p>Your given XML source contains some errors; I fixed those, see my source below:</p>

<pre><code>from lxml import etree

source = """
&lt;root&gt;
  &lt;scene name="scene1"&gt;
    &lt;view ath="0" atv="10" /&gt;
    &lt;image url="img1.jgp" /&gt;
    &lt;hotspot name="hot1" /&gt;
  &lt;/scene&gt;

  &lt;scene name="scene2"&gt;
    &lt;view ath="20" atv="10" /&gt;
    &lt;image url="img2.jgp" /&gt;
    &lt;hotspot name="hot2" /&gt;
  &lt;/scene&gt;
&lt;/root&gt;
"""
</code></pre>

<p>To parse this source you will create an etree:</p>

<pre><code>tree = etree.fromstring(source)
</code></pre>

<p>(For source coming from a file, use <code>etree.parse()</code> instead.)</p>

<p>Now you can browse through the parsed XML by accessing <code>tree</code> properly.  My favorite way of doing so is by navigating with XPaths (mastering these is out of scope of your question):</p>

<pre><code>allViews = tree.xpath('//root/scene/view')
for view in allViews:
  print view.attrib
</code></pre>

<p>This will print all XML attributes for each view tag found by the XPath:</p>

<pre><code>{'atv': '10', 'ath': '0'}
{'atv': '10', 'ath': '20'}
</code></pre>

<p>Of course you can also access the other attributes of the view elements like their embedded text (which is empty here of course) or their subelements (children) (of course, in your example they also do not have children).</p>

<p>The wording of your question suggests that you might not have built up an understanding of the fact that this <code>view</code> object is indeed "the entire view tag".  You can ask the <code>view</code> object for the tag it is made up of (<code>view</code>), for its attributes (see above), its contents (<code>view.text</code>) and even its subelements (<code>view.getchildren()</code>, but there are none).</p>

<p>You can convert the parsed XML structure back to an ASCII representation by calling <code>etree.tostring(view)</code>; this will return a string like <code>'&lt;view ath="20" atv="10"/&gt;\n    '</code>.  In most cases you will not do this.</p>

<p>You can also access the elements view the children of the elements:</p>

<pre><code>print tree.getchildren()[1].getchildren()[0].attrib
</code></pre>

<p>This will print the XML attributes of the 0th child (a <code>view</code>) of the first child (a <code>scene</code>) of the <code>tree</code> element:</p>

<pre><code>{'atv': '10', 'ath': '20'}
</code></pre>

python xml lxml