Bhaskar Bhaskar - 1 year ago 25
Python Question

Extracting particular tags from an XML and write to a new XML in Python

I have an xml like this:

<Manuscript>
<model defaultValue="data.TotalResult">
<object id="data" path="data">
<condition>
<comparison compare="and">
<operand idref="Context1" type="boolean" />
<operand idref="Context2" type="boolean" />
</comparison>
</condition>

</object>
<condition>
<comparison compare="and">
<operand idref="Context5" type="boolean" />
<operand idref="Context6" type="boolean" />
</comparison>
</condition>
</model>
<condition>
<comparison compare="and">
<operand idref="Context9" type="boolean" />
<operand idref="Context10" type="boolean" />
</comparison>
</condition>

</Manuscript>


I want to extract all the tags having name as 'condition', concatenate/append them together and create another xml as:

<root>
<condition>
<comparison compare="and">
<operand idref="Context1" type="boolean" />
<operand idref="Context2" type="boolean" />
</comparison>
</condition>
<condition>
<comparison compare="and">
<operand idref="Context5" type="boolean" />
<operand idref="Context6" type="boolean" />
</comparison>
</condition>
<condition>
<comparison compare="and">
<operand idref="Context9" type="boolean" />
<operand idref="Context10" type="boolean" />
</comparison>
</condition>
</root>


Any idea how can i accomplish this is Python ?

Thanks in advance.

Answer Source

You can use BeautifulSoup for both parsing and creating the new xml that you want.

Check out the following script and change it as you wish in order to achieve the desired outcome.

from bs4 import BeautifulSoup as Soup

xml_str = """
<Manuscript>
<model defaultValue="data.TotalResult">
    <object id="data" path="data">
        <condition>
              <comparison compare="and">
                <operand idref="Context1" type="boolean" />
                <operand idref="Context2" type="boolean" />
              </comparison>
        </condition>

    </object>
    <condition>
              <comparison compare="and">
                <operand idref="Context5" type="boolean" />
                <operand idref="Context6" type="boolean" />
              </comparison>
    </condition>
</model>
    <condition>
              <comparison compare="and">
                <operand idref="Context9" type="boolean" />
                <operand idref="Context10" type="boolean" />
              </comparison>
    </condition>
</Manuscript>
"""

xml_parsed = Soup(xml_str, 'lxml')
output_xml = Soup("", 'lxml')
output_xml.append(output_xml.new_tag('root'))
for condition in xml_parsed.find_all('condition'):
    output_xml.find('root').append(condition)
print(output_xml.prettify())

This will print the following:

<root>
 <condition>
  <comparison compare="and">
   <operand idref="Context1" type="boolean">
   </operand>
   <operand idref="Context2" type="boolean">
   </operand>
  </comparison>
 </condition>
 <condition>
  <comparison compare="and">
   <operand idref="Context5" type="boolean">
   </operand>
   <operand idref="Context6" type="boolean">
   </operand>
  </comparison>
 </condition>
 <condition>
  <comparison compare="and">
   <operand idref="Context9" type="boolean">
   </operand>
   <operand idref="Context10" type="boolean">
   </operand>
  </comparison>
 </condition>
</root>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download