1

I have an XML file as:

<a>
    <b>
        <c>
            <condition>
            ....

            </condition>
        </c>
    </b>
</a>

I have another XML in string type as :

<condition>
  <comparison compare="and">
    <operand idref="Agent" type="boolean" />
    <comparison compare="lt">
      <operand idref="Premium" type="float" />
      <operand type="int" value="10000" />
    </comparison>
  </comparison>
</condition>

I need to comment the 'condition block' in the first xml and then append this second xml in place of it. I did not try to comment the first block but tried to append the second xml in the first. I am able to append it to it but I am getting the '<' and '>' as &lt ; and &gt ; respectively as

<a>
    <b>
        <c>
            <condition>
            ....

            </condition>

                &lt;condition&gt;
 &lt;comparison compare="and"&gt;
&lt;operand idref="Agent" type="boolean"/&gt;
&lt;comparison compare="lt"&gt;
  &lt;operand idref="Premium" type="float"/&gt;
  &lt;operand type="int" value="10000"/&gt;
&lt;/comparison&gt;
&lt;/comparison&gt;
&lt;/condition&gt;

How do I convert this back to < and > rather than lt and gt?

And how do I delete or comment the <condition> block of the first xml below which I will append the new xml?

tree = ET.parse('basexml.xml')  #This is the xml where i will append
tree1 = etree.parse(open('newxml.xml'))  # This is the xml to append
xml_string = etree.tostring(tree1, pretty_print = True)  #converted the xml to string
tree.find('a/b/c').text = xml_string #updating the content of the path with this new string(xml)

I converted the 'newxml.xml' into a string 'xml_string' and then appended to the path a/b/c of the first xml

3
  • XMLs are easier to manipulate with a library designed for that. try: crummy.com/software/BeautifulSoup/bs4/doc May 25, 2017 at 10:26
  • tree = etree.parse(open('basexml.xml') #This is the xml where i will append May 25, 2017 at 10:46
  • @mzjn tree = ET.parse('basexml.xml') #This is the xml where i will append tree1 = etree.parse(open('newxml.xml')) # This is the xml to append xml_string = etree.tostring(tree1, pretty_print = True) tree.find('a/b/c').text = xml_string I converted the 'newxml.xml' into a string 'xml_string' and then appended to the path a/b/c of the first xml May 25, 2017 at 10:55

1 Answer 1

2

You are adding newxml.xml, as a string, to the text property of the <c> element. That does not work. You need to add an Element object as a child of <c>.

Here is how it can be done:

from xml.etree import ElementTree as ET

# Parse both files into ElementTree objects
base_tree = ET.parse("basexml.xml")
new_tree = ET.parse("newxml.xml")

# Get a reference to the "c" element (the parent of "condition")
c = base_tree.find(".//c")

# Remove old "condition" and append new one
old_condition = c.find("condition")
new_condition = new_tree.getroot()
c.remove(old_condition)
c.append(new_condition)

print ET.tostring(base_tree.getroot())

Result:

<a>
  <b>
    <c>
      <condition>
  <comparison compare="and">
    <operand idref="Agent" type="boolean" />
    <comparison compare="lt">
      <operand idref="Premium" type="float" />
      <operand type="int" value="10000" />
    </comparison>
  </comparison>
</condition></c>
  </b>
</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.