即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

python xml parse and modify node

编程语言 wenshifang 26℃ 0评论
from xml.etree.ElementTree import ElementTree, Element

def is_match(node, kv_map)
    for key in kv_map
        if ( node.get(key) != kv_map.get(key):
            return False
    return True
def modify_node(nodelist, kv_map)
    for node in nodelist:
        node.set(key, kv_map.get(key))

if __name__ == '__main__'
    tree = ElementTree()
    tree.parse("version.xml")
    nodelist = tree.findall("product/product")

    result_nodes = []
    for node in nodelist:
        if is_match(node, {"id:0000"}):
            result_nodes.append(node)

    modify_node(result_nodes, "least":"0.0.2")
    tree.write("new_version.xml", encoding='utf-8',xml_declaratioin=True)

转载请注明:CodingBlog » python xml parse and modify node

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情