0

I need to modify the parameters of a function in xml file with ElementTree while the elements have similar names. In this example I want to change only the number 2 and keep the others. Is it possible?

<Model>
    <Function>
      <param>x</param>
      <param>type</param>
      <param>2</param>
      <param>5</param>
     </Function>
</Model>
1
  • you can use xpath to select the correct param-element based on the value 2 (see here). alternatively you could also select the third occurrence of param (see here). once you have the element, use ElementTrees capabilities to change the text (see here). Commented Feb 21, 2020 at 14:43

1 Answer 1

0

Just one example, though a little late.

from simplified_scrapy import SimplifiedDoc, utils, req
html = '''
<Model>
    <Function>
      <param>x</param>
      <param>type</param>
      <param>2</param>
      <param>5</param>
     </Function>
</Model>
'''
doc = SimplifiedDoc(html)
# Use text 2
param = doc.getElementByText('2',tag='param')
# Use index
param = doc.selects('param')[2]
param.setContent('Modified')
print(doc.html)

Result:

<Model>
    <Function>
      <param>x</param>
      <param>type</param>
      <param>Modified</param>
      <param>5</param>
     </Function>
</Model>
Sign up to request clarification or add additional context in comments.

Comments

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.