免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 12306 | 回复: 3
打印 上一主题 下一主题

ElementTree的xml增加子节点,求大神帮忙 [复制链接]

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-05 17:05 |只看该作者 |倒序浏览
本帖最后由 代号:军刀 于 2013-09-05 17:17 编辑

test.xml
  1. <?xml version="1.0"?>
  2. <data>
  3.   <platform name="A">
  4.       <server id="1" ip="192.168.19.103" />
  5.       <server id="3" ip="192.168.19.103" />
  6.       <server id="4" ip="192.168.19.102" />
  7.   </platform>
  8.   <platform name="B">
  9.       <server id="1" ip="8.8.8.8" />
  10.   </platform>
  11. </data>
复制代码
现在遇到的问题是,我现在老是不能把数据插到正确的位置,比如数据为:<server id="9" ip="1.2.3.4" />
  1. #!/usr/bin/python
  2. #coding=utf-8

  3. from xml.etree.ElementTree import ElementTree,Element

  4. def read_xml(in_path):
  5.     tree=ElementTree()
  6.     tree.parse("test.xml")
  7.     return tree

  8. def write_xml(tree,out_path):
  9.     tree.write(out_path)

  10. def if_match(node,kv_map):
  11.     for key in kv_map:
  12.         if node.get(key) != kv_map.get(key):
  13.             return False
  14.     return True

  15. def find_nodes(tree,path):
  16.     return tree.findall(path)
  17. def get_node_by_keyvalue(nodelist,kv_map):
  18.     result_nodes=[]
  19.     for node in nodelist:
  20.         if if_match(node,kv_map):
  21.             result_nodes.append(node)
  22.     return result_nodes
  23. def create_node(tag,property_map):
  24.     element=Element(tag,property_map)
  25.     return element

  26. def add_child_node(nodelist,element):
  27.     for node in nodelist:
  28.         node.append(element)

  29. if __name__ == "__main__":

  30.     #读取xml文件
  31.     tree=read_xml("./test.xml")

  32.     #属性修改
  33.     nodes=find_nodes(tree,"platform")
  34.     result_nodes=get_node_by_keyvalue(nodes,{"name":"A"})
  35.     a=create_node("server",{"id":"9","ip":"1.2.3.4"})
  36.     add_child_node(result_nodes,a)
  37.     write_xml(tree,"test.xml")
复制代码
我的代码运行后xml的格式如下:
  1. <data>
  2.   <platform name="A">
  3.       <server id="1" ip="192.168.19.103" />
  4.       <server id="3" ip="192.168.19.103" />
  5.       <server id="4" ip="192.168.19.102" />
  6.   <server id="9" ip="1.2.3.4" /></platform>
  7.   <platform name="B">
  8.       <server id="1" ip="8.8.8.8" />
  9.   </platform>
  10. </data>
复制代码
我想要的结果是:
  1. <data>
  2.   <platform name="A">
  3.       <server id="1" ip="192.168.19.103" />
  4.       <server id="3" ip="192.168.19.103" />
  5.       <server id="4" ip="192.168.19.102" />
  6.       <server id="9" ip="1.2.3.4" />
  7.   </platform>
  8.   <platform name="B">
  9.       <server id="1" ip="8.8.8.8" />
  10.   </platform>
  11. </data>
复制代码
求大神帮忙

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2013-09-05 18:06 |只看该作者
两者区别就是少了几个换行或TAB或空格吧,把它们当作xml的text node,用ET创建之,然后添加到<platform>下就完事了。。

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
3 [报告]
发表于 2013-09-05 18:31 |只看该作者
还是写个例子吧。要达到你的效果,就需要自己动手排列组合一番了,进一步参考Element Tree官方文档
  1. Python 2.6.6 (r266:84292, Jul 10 2013, 22:43:23)
  2. [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import xml.etree.ElementTree as ET
  5. >>> root = ET.fromstring('<a><b>hello</b></a>')
  6. >>> ET.dump(root)
  7. <a><b>hello</b></a>
  8. >>> root.text = "  "
  9. >>> b = root.find('b')
  10. >>> b.tail = '\n\t'
  11. >>> ET.dump(root)
  12. <a>  <b>hello</b>
  13.         </a>
复制代码

论坛徽章:
13
丑牛
日期:2013-08-16 15:08:22技术图书徽章
日期:2013-11-26 10:13:40双鱼座
日期:2013-11-08 15:03:26戌狗
日期:2013-11-08 13:52:30技术图书徽章
日期:2013-11-05 14:06:30戌狗
日期:2013-10-31 11:45:42CU十二周年纪念徽章
日期:2013-10-24 15:41:34天秤座
日期:2013-10-11 14:55:08子鼠
日期:2013-09-26 19:36:35水瓶座
日期:2013-09-26 17:44:56午马
日期:2013-08-26 10:24:23丑牛
日期:2013-08-19 14:43:22
4 [报告]
发表于 2013-09-05 19:55 |只看该作者
回复 3# timespace


    大神为什么不帮我指点下我的代码哪里有问题呢?按照ElementTree官网的构建树结构是不会出现说少tab,回车这类的东西的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP