使用Python查找和替换XML中的值 [英] Find and Replace Values in XML using Python

查看:392
本文介绍了使用Python查找和替换XML中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用python编辑XML文件。我想查找和替换标签中的关键字。过去,一名同事建立了模板XML文件,并使用查找和替换程序来替换这些关键词。我想用Python来查找和替换这些关键字的值。我一直在教自己的Elementtree模块,但我很难找到和替换。我已经附加了我的XML文件的一点点。你会看到一些由%(即%SITEDESCR%)包围的变量这些是我想要替换的单词,然后将XML保存到一个新的文件中。任何帮助或建议将是伟大的。



谢谢,
麦克

 <元数据> 
< idinfo>
< citation>
< citeinfo>
< origin>我的公司< / origin>
< pubdate> 05/04/2009< / pubdate>
< title> POLYGONS< / title>
< geoform>矢量数字数据< / geoform>
< onlink> \\C $ \ArcGISDevelopment\Geodatabase\PDA_STD_05_25_2009.gdb< / onlink>
< / citeinfo>
< / citation>
<描述>
< abstract>这个数据集表示从字段数据为%SITEDESCR%开发的映射多边形。< / abstract>
< purpose>这个数据集是为了配合一些东西而创建的。< / purpose>
< / description>
< timeperd>
< timeinfo>
< rngdates>
< />>%begdate%< / begdate>
< begtime>未知< / begtime>
< enddate>%enddate%< / enddate>
< endtime>未知< / endtime>
< / rngdates>
< / timeinfo>
<当前>地面状况< /当前>
< / timeperd>


解决方案

基本知识:

  from xml.etree导入ElementTree作为et 
tree = et.parse(datafile)
tree.find('idinfo / timeperd / timeinfo /rngdates/begdate').text ='1/1/2011'
tree.find('idinfo / timeperd / timeinfo / rngdates / enddate')。text ='1/1/2011'
tree.write(datafile)

如果标签名称是唯一的,则可以缩短路径。这个语法找到树中任何深度级的第一个节点。

  tree.find('.// begdate')。 text ='1/1/2011'
tree.find('.// enddate')。text ='1/1/2011'

另外,请阅读文档, ESP。用于定位节点的 XPath 支持。


I am looking to edit XML files using python. I want to find and replace keywords in the tags. In the past, a co-worker had set up template XML files and used a "find and replace" program to replace these key words. I want to use python to find and replace these key words with values. I have been teaching myself the Elementtree module, but I am having trouble trying to do a find and replace. I have attached a snid-bit of my XML file. You will seen some variables surrounded by % (ie %SITEDESCR%) These are the words I want to replace and then save the XML to a new file. Any help or suggestions would be great.

Thanks, Mike

<metadata>
<idinfo>
<citation>
<citeinfo>
 <origin>My Company</origin>
 <pubdate>05/04/2009</pubdate>
 <title>POLYGONS</title>
 <geoform>vector digital data</geoform>
 <onlink>\\C$\ArcGISDevelopment\Geodatabase\PDA_STD_05_25_2009.gdb</onlink>
</citeinfo>
</citation>
 <descript>
 <abstract>This dataset represents the mapped polygons developed from the field data for the %SITEDESCR%.</abstract>
 <purpose>This dataset was created to accompany some stuff.</purpose>
 </descript>
<timeperd>
<timeinfo>
<rngdates>
 <begdate>%begdate%</begdate>
 <begtime>unknown</begtime>
 <enddate>%enddate%</enddate>
 <endtime>unknown</endtime>
 </rngdates>
 </timeinfo>
 <current>ground condition</current>
 </timeperd>

解决方案

The basics:

from xml.etree import ElementTree as et
tree = et.parse(datafile)
tree.find('idinfo/timeperd/timeinfo/rngdates/begdate').text = '1/1/2011'
tree.find('idinfo/timeperd/timeinfo/rngdates/enddate').text = '1/1/2011'
tree.write(datafile)

You can shorten the path if the tag name is unique. This syntax finds the first node at any depth level in the tree.

tree.find('.//begdate').text = '1/1/2011'
tree.find('.//enddate').text = '1/1/2011'

Also, read the documentation, esp. the XPath support for locating nodes.

这篇关于使用Python查找和替换XML中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆