【python】修改xml文件

例如Hadoop的hdfs-site.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>dfs.replication</name>
    <value>3</value>
  </property>
  <property>
    <name>dfs.namenode.name.dir</name>
    <value>file:///home/hadoop-2.7.7/dfs/nn</value>
  </property>
  <property>    
     <name>dfs.datanode.data.dir</name>    
     <value>file:///home/hadoop-2.7.7/dfs/dn</value>  
  </property>
  <property>
    <!-- HDFS blocksize of 256MB for large file-systems.  default 128MB-->
    <name>dfs.blocksize</name>
    <value>268435456</value>
  </property>

  <property>
   <name>dfs.secondary.http.address</name>
   <value>node1:50090</value>
   <description>secondarynamenode</description>
  </property>

</configuration>

修改xml已有的值

现在要使用程序修改配置,配置dfs.secondary.http.address的值node1:50090修改为slave1:50090

#_*_coding:UTF-8_*_
# 上面这行是解决py文件不支持中文的解决办法

# import xml tool
import xml.etree.ElementTree as et

def set_hdfs_site_xml():
	# parse xml
	doc = et.parse('/home/hadoop-2.7.7/etc/hadoop/hdfs-site.xml')
	# root is <configuration>
    root = doc.getroot()
    # ps is a list of tag '<property>'.
    ps = root.iter('property')
    for p in ps:
    	# p is one <property>. p[0] is <name>.p[1] is <value>
    	# p[0].tag is 'name';p[1].tag is 'value'
    	
    	# find the config_name and change the config_value
        if ('dfs.secondary.http.address'.__eq__(p[0].text)):
        	# p[0].text is 'dfs.secondary.http.address';p[1].text is 'node1:50090'
        	# change 'node1:50090' to 'slave1:50090'
            p[1].text = 'slave1:50090'
    # save the new xml. the path should be set
    doc.write('/home/hadoop-2.7.7/etc/hadoop/hdfs-site.xml')


xml增加新的标签内容

<property>
  <name>new.conf</name>
  <value>happy new year</value>
</property>
def vi_xml():
    doc = et.parse(xml_path)
    # root is <configuration>
    root = doc.getroot()

	# new '<property>'
    np = et.SubElement(root, 'property', attrib={})
    # add new '<name>' in <property>
    nn = et.SubElement(np, 'name', attrib={})
    # set text
    nn.text = 'new.conf'
    # add new '<value>' in <property>
    nv = et.SubElement(np,'value', attrib={})
    # set text
    nv.text = 'happy new year'
    # print
    et.dump(np)
	
	# add the new <property> in <configuration>
    root.append(np)
    doc.write('output.xml')


if __name__ == '__main__':
    vi_xml()
测试发现添加的新标签内容没有换行,但是内容是正确的
<property><name>new.conf</name><value>happy new year</value></property><property><name>new.conf</name><value>happy new year</value></property></configuration>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值