find xml element based on its attribute and change its value

后端 未结 4 981
清歌不尽
清歌不尽 2021-02-01 08:44

I am using python xmlElementTree and want to assign or modify a xml element value based on its attribute. Can somebody give me an idea how to do this?

For example: Here

4条回答
  •  醉梦人生
    2021-02-01 09:07

    larsks explains how to use XPath to find what you are after very well. You also wanted to change an attribute. The best way is probably to add a new attribute and remove the original. Once you get the nodes result, it is a list with a single entry (number).

    # This returns sys/phoneNumber/1
    nodes[0].get("topic")
    # To change the value, use set 
    nodes[0].set("topic", "new/value/of/phone/number")
    

    Hope this helps.

    Also, your ending root tag doesn't close properly.

提交回复
热议问题