ElementTree解析xml文件的一些疑问

Python/PHP/Perl 开发与设计
回复
头像
zhanju7hao
帖子: 32
注册时间: 2012-09-24 14:08
系统: ubuntu 12.10
送出感谢: 4 次
接收感谢: 0

ElementTree解析xml文件的一些疑问

#1

帖子 zhanju7hao » 2016-08-26 15:31

xml文件如下:

代码: 全选

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
<property>
        <name>fs.defaultFS</name>
        <value>hdfs://adh1:9000</value>
</property>
<property>
  <name>hadoop.proxyuser.root.hosts</name>
    <value>*</value>
    </property>
<property>
      <name>hadoop.proxyuser.root.groups</name>
        <value>*</value>
</property>
<property>  
      <name>hadoop.proxyuser.httpfs.hosts</name>  
      <value>*</value>  
</property>
        <property>  
                <name>hadoop.proxyuser.httpfs.groups</name>  
                <value>*</value>  
        </property> 
</configuration>
python脚本如下:

代码: 全选

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 25 16:44:16 2016

@author: shuangyu
"""
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
    

#读取配置信息生成字典
def getconfig():
    dic1 = {}
    for ch in root.getchildren():
        k =[]
        v =[]
        for x in ch.getchildren():
            #print x.tag,x.text
            if x.tag == 'name':
                k.append(x.text)
            elif x.tag == 'value':
                v.append(x.text)
                dic1[k[0]] = v[0]
    
    return dic1


#增加一个节点
def create_node(name, value):
    nodep = ET.Element('property')
    noden = ET.Element('name')
    noden.text = name
    nodev = ET.Element('value')
    nodev.text = value
    nodep.append(noden)
    nodep.append(nodev)
    return nodep
    
def add_node(chnode,rnode):
    rnode.append(chnode)
    
#修改节点
#print root.getchildren()[0].getchildren()[0].text 
def chanage_value(name,new_value):
    for p in root.getchildren():
        if name in [x.text for x in p.iter('name')]:
            for v in p.getchildren():
                if v.tag == 'value':
                    v.text = new_value
#删除节点
def del_node(name):
    for x in root.getchildren():
        if name in [i.text for i in x.iter('name')]:
            root.remove(x)

if __name__ == '__main__':
    tree = ET.parse('C:\Users\shuangyu\.spyder2\core-site.xml')
    root = tree.getroot()
    print "xml文件,原信息: "
    ET.dump(root)
    print '#' * 50
    print '新增一个节点 name=shuangyu ,value =2016 '
    chnode = create_node('shuangyu','2016')
    add_node(chnode,root)
    print "新增后的xml文件"
    ET.dump(root)
    print '#' * 50
    print "修改hadoop.proxyuser.httpfs.groups 的value为bigdata"
    chanage_value('hadoop.proxyuser.httpfs.groups','bigdata')
    ET.dump(root)
    print '#' * 50
    print "删除节点hadoop.proxyuser.httpfs.hosts"
    del_node('hadoop.proxyuser.httpfs.hosts')
    ET.dump(root)    
疑问如下:
1.发现新增的节点中并没有换行输出,都是在同一行中<property><name>shuangyu</name><value>2016</value></property>,这个要如何使其换行输出呢,希望输出的格式如下:
<property>
<name>shuangyu</name>
<value>2016</value>
</property>

2.修改节点的text的方法,如何在优化下,查找到对应的name,然后修改掉该name同级的value的值……
或者有什么方法能够快速定位到该name的索引位置及对应的value的索引位置?

:Effort :Effort :Effort
回复

回到 “Python/Php/Perl”