2

I have an xml that I parsed with objectify from an API output and i refer to it as "result" variable. Now I want o keep the object, but only change the text file and give it back to the API to update the element.

<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="multi_text_area">
  <id>1754</id>
  <name>Devices under maintenance</name>
  <read_only>false</read_only>
  <text_area>
    <text>defwhanld12x</text>
  </text_area>
</field>

When I try to change the text I get like this, I get an error:

result.text_area.text = 'This is a test'

TypeError: attribute 'text' of 'ObjectifiedElement' objects is not writable

I also tried to strip the element and recreate it as the lxml documentation says that you can not change an object.

etree.strip_elements(result, 'text')
etree.SubElement(result.text_area, 'text').text = 'This is just a test'

But get a similar error:

TypeError: attribute 'text' of 'StringElement' objects is not writable

1 Answer 1

2

This is because your element is named text. text is also used by lxml.objectify to store inner text of an element, and that's how the conflict happened. When you do result.text_area.text, it is interpreted as trying to access the inner text of text_area, instead of accessing child element named text. You can avoid this conflict by accessing the text element as follow :

result.text_area['text'] = 'This is a test'

UPDATE :

The above turned out to be replacing the entire <text> element with the new text, which end up as an element in the form you mentioned in the comment below :

<text xmlns:py="http://codespeak.net/lxml/objectify/pytype" 
      py:pytype="str">This is a test</text>

The correct way to update inner text of text element would be using _setText(), as mentioned in this other answer :

result.text_area['text']._setText('This is a test')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot that worked! But my response looks like <field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="multi_text_area"> <id>1754</id> <name>Devices under maintenance</name> <read_only>false</read_only> <text_area> <text xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">This is a test</text> </text_area> </field> I tried to strip the it away with etree.cleanup_namespaces(field.text_area['text']) but that did not work.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.