Pythonxml属性节点⽂本的增删改[ElementTree]博客已迁移,
========================
需求:
在实际应⽤中,需要对xml配置⽂件进⾏实时修改,
1.增加、删除 某些节点
2.增加,删除,修改某个节点下的某些属性
3.增加,删除,修改某些节点的⽂本
xml源⽂件格式[例]
<?xml version="1.0" encoding="UTF-8"?>
<framework>
<processers>
<processer name="AProcesser" file="lib64/A.so"
path="/tmp">
</processer>
<processer name="BProcesser" file="lib64/B.so" value="fordelete">
</processer>
<processer name="BProcesser" file="lib64/B.so2222222"/>
<services>
<service name="search" prefix="/bin/search?"
output_formatter="OutPutFormatter:service_inc">
<chain sequency="chain1"/>
<chain sequency="chain2"></chain>
</service>
<service name="update" prefix="/bin/update?">
<chain sequency="chain3" value="fordelete"/>
</service>
</services>
</processers>
</framework>
使⽤库:
实现思想:
使⽤ElementTree,先将⽂件读⼊,解析成树,之后,根据路径,可以定位到树的每个节点,再对节点进⾏修改,最后直接将其输出
代码附⽂档:
#!/usr/bin/python
# -*- coding=utf-8 -*-
# author : wklken@yeah
# author : wklken@yeah
# date: 2012-05-25
# version: 0.1
ElementTree import ElementTree,Element
def read_xml(in_path):
'''读取并解析xml⽂件
in_path: xml路径
return: ElementTree'''
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
'''将xml⽂件写出
tree: xml树
out_path: 写出路径'''
tree.write(out_path, encoding="utf-8",xml_declaration=True)
def if_match(node, kv_map):
'''判断某个节点是否包含所有传⼊参数属性
node: 节点
kv_map: 属性及属性值组成的map'''
for key in kv_map:
(key) != (key):
return False
return True
#---------------search -----
def find_nodes(tree, path):
'''查某个路径匹配的所有节点
tree: xml树
path: 节点路径'''
return tree.findall(path)
def get_node_by_keyvalue(nodelist, kv_map):
'''根据属性及属性值定位符合的节点,返回节点
nodelist: 节点列表
kv_map: 匹配属性及属性值map'''
result_nodes = []
for node in nodelist:
if if_match(node, kv_map):
result_nodes.append(node)
return result_nodes
#---------------change -----
def change_node_properties(nodelist, kv_map, is_delete=False):
'''修改/增加 /删除节点的属性及属性值
nodelist: 节点列表
kv_map:属性及属性值map'''
for node in nodelist:
for key in kv_map:
if is_delete:
if key in node.attrib:
del node.attrib[key]
else:
node.set(key, (key))
def change_node_text(nodelist, text, is_add=False, is_delete=False): '''改变/增加/删除⼀个节点的⽂本
nodelist:节点列表
text : 更新后的⽂本'''
text : 更新后的⽂本'''
for node in nodelist:
if is_add:
< += text
elif is_delete:
< = ""
else:
< = text
def create_node(tag, property_map, content):
'''新造⼀个节点
tag:节点标签
property_map:属性及属性值map
content: 节点闭合标签⾥的⽂本内容
return 新节点'''
element = Element(tag, property_map)
< = content
return element
def add_child_node(nodelist, element):
'''给⼀个节点添加⼦节点
nodelist: 节点列表
element: ⼦节点'''
for node in nodelist:
node.append(element)
def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
'''同过属性及属性值定位⼀个节点,并删除之
nodelist: ⽗节点列表
tag:⼦节点标签
kv_map: 属性及属性值列表'''
for parent_node in nodelist:
children = hildren()
for child in children:
if child.tag == tag and if_match(child, kv_map):
ve(child)
if __name__ == "__main__":
#1. 读取xml⽂件
tree = read_xml("./l")
#2. 属性修改
#A. 到⽗节点
nodes = find_nodes(tree, "processers/processer")
#B. 通过属性准确定位⼦节点
result_nodes = get_node_by_keyvalue(nodes, {"name":"BProcesser"})
#C. 修改节点属性
change_node_properties(result_nodes, {"age": "1"})
#D. 删除节点属性
change_node_properties(result_nodes, {"value":""}, True)
#3. 节点修改
#A.新建节点
a = create_node("person", {"age":"15","money":"200000"}, "this is the firest content")
#B.插⼊到⽗节点之下
add_child_node(result_nodes, a)
#4. 删除节点
#定位⽗节点
del_parent_nodes = find_nodes(tree, "processers/services/service")
#准确定位⼦节点并删除之
target_del_node = del_node_by_tagkeyvalue(del_parent_nodes, "chain", {"sequency" : "chain1"})
#5. 修改节点⽂本
#定位节点
text_nodes = get_node_by_keyvalue(find_nodes(tree, "processers/services/service/chain"), {"sequency":"chain3"}) change_node_text(text_nodes, "new text")
#6. 输出到结果⽂件
write_xml(tree, "./l")
通过main处理后的结果⽂件:
<?xml version='1.0' encoding='utf-8'?>
<framework>
<processers>
<processer file="lib64/A.so" name="AProcesser" path="/tmp">
python处理xml文件</processer>
<processer age="1" file="lib64/B.so" name="BProcesser">
<person age="15" money="200000">this is the firest content</person>
</processer>
<processer age="1" file="lib64/B.so2222222" name="BProcesser">
<person age="15" money="200000">this is the firest content</person>
</processer>
<services>
<service name="search" output_formatter="OutPutFormatter:service_inc"
prefix="/bin/search?">
<chain sequency="chain2" />
</service>
<service name="update" prefix="/bin/update?">
<chain sequency="chain3" value="fordelete">new text</chain>
</service>
</services>
</processers>
</framework>
The end!
wklken
2012-05-25
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论