Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract complete XML block using python

Is it possible to extract complete blocks of XML text from an XML file using Python? I am using ElementTree with Python to extract tags and values from XML, in order to compare 2 XML files. But is it possible to extract the whole text of an XML block?

For example:

<stats>
<player>
    <name>Luca Toni</name>
    <matches>47</matches>
    <goals>16</goals>
    <WC>yes</WC>
</player>
<player>
    <name>Alberto Gilardino</name>
    <matches>57</matches>
    <goals>19</goals>
    <WC>yes</WC>
</player>
<player>
    <name>Mario Balotelli</name>
            <matches>36</matches>
            <goals>14</goals>
            <WC>yes</WC>
</player>
</stats>

Is it possible to extract one particular complete block (), as given below, from the above XML using python (ElementTree)?

<player>
    <name>Luca Toni</name>
    <matches>47</matches>
    <goals>16</goals>
    <WC>yes</WC>
</player>
like image 682
Timothy Henry Avatar asked Feb 20 '26 00:02

Timothy Henry


1 Answers

Once you've parsed your document with etree, you can do several things

import xml.etree.ElementTree  as ET

doc = ET.parse('test.xml')
root = doc.getroot()

print(root.find("player"))                  # get first player
print(root.find(".//player"))               # get first player if it's not a direct child
print([p for p in root.findall("player")])  # get all players (direct children)
print([p for p in root.getchildren()])      # get direct children

getting an element as a string is just

test = ET.tostring(root.find("player"))
print(text)

EDIT note that to compare elements, this is not necessarily the best method. See here for another option.

like image 105
Silmathoron Avatar answered Feb 22 '26 13:02

Silmathoron