【问题标题】:Find and Replace tags in XML using Python使用 Python 在 XML 中查找和替换标签
【发布时间】:2011-08-02 21:22:08
【问题描述】:

我之前提出过一个类似的问题,但这个问题略有不同。我想使用 python 查找和替换 XML 标签。我正在使用 XML 上传一些 GIS shapefile 的元数据。在元数据编辑器中,我可以选择收集某些数据的日期。选项是“单个日期”、“多个日期”和“日期范围”。在包含一系列日期标签的第一个 XML 中,您将看到标签“rngdates”以及一些子元素“begdate”、“begtime”、“enddate”和。我想编辑这些标签,使其看起来像包含多个单个日期的第二个 XML。新标签是“mdattim”、“sngdate”和“caldate”。我希望这足够清楚,但如果需要,请询问更多信息。 XML 是一头奇怪的野兽,我还没有完全理解它。

谢谢, 迈克

第一个 XML:

<idinfo>
  <citation>
    <citeinfo>
       <origin>My Company Name</origin>
       <pubdate>05/04/2009</pubdate>
       <title>Feature Class Name</title>
       <edition>0</edition>
       <geoform>vector digital data</geoform>
       <onlink>.</onlink>
     </citeinfo>
   </citation>
<descript>
  <abstract>This dataset represents the GPS location of inspection points collected in the field for the Site Name</abstract>
  <purpose>This dataset was created to accompany the clients Assessment Plan. This point feature class represents the location within the area that the field crews collected related data.</purpose>
 </descript>
<timeperd>
 <timeinfo>
   <rngdates>
     <begdate>7/13/2010</begdate>
     <begtime>unknown</begtime>
     <enddate>7/15/2010</enddate>
     <endtime>unknown</endtime>
    </rngdates>
 </timeinfo>
 <current>ground condition</current>
</timeperd>

第二个 XML:

<idinfo>
  <citation>
    <citeinfo>
      <origin>My Company Name</origin>
      <pubdate>03/07/2011</pubdate>
      <title>Feature Class Name</title>
      <edition>0</edition>
      <geoform>vector digital data</geoform>
      <onlink>.</onlink>
    </citeinfo>
   </citation>
 <descript>
   <abstract>This dataset represents the GPS location of inspection points collected in the field for the Site Name</abstract>
   <purpose>This dataset was created to accompany the clients Assessment Plan. This point feature class represents the location within the area that the field crews collected related data.</purpose>
 </descript>
<timeperd>
 <timeinfo>
  <mdattim>
    <sngdate>
      <caldate>08-24-2009</caldate>
      <time>unknown</time>
     </sngdate>
    <sngdate>
      <caldate>08-26-2009</caldate>
    </sngdate>
   <sngdate>
      <caldate>08-26-2009</caldate>
    </sngdate>
   <sngdate>
      <caldate>07-07-2010</caldate>
    </sngdate>
  </mdattim>
</timeinfo>

到目前为止,这是我的 Python 代码:

folderPath = "Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009"

for filename in glob.glob(os.path.join(folderPath, "*.xml")):

    fullpath = os.path.join(folderPath, filename)

    if os.path.isfile(fullpath):
        basename, filename2 = os.path.split(fullpath)

        root = ElementTree(file=r"Z:\ESRI\Figure_Sourcing\Figures\Metadata\Run_Metadata_2009\\" + filename2)

        iter = root.getiterator()
        #Iterate
        for element in iter:
            print element.tag

            if element.tag == "begdate":
                element.tag.replace("begdate", "sngdate")

【问题讨论】:

  • 另外,向我们展示将一种转换为另一种的规则。 IE。显示输入和从该输入生成的预期输出。
  • 第一个 XML 是输入。我有许多模板 XML,它们在某些标签之间嵌入了关键字。第二个是我手动编辑的输出。我想编辑第一个 XML,以便第一个 XML 中 timeinfo 标记之间的所有内容都替换为第二个 XML 中这些相同标记之间的所有内容。我正在使用 Python,因为这是一个 ArcGIS 函数,而 Python 是首选语言。我将此脚本与他们的 python 工具结合使用。我的脚本将用于批处理 XML,以用作大量 GIS shapefile 中的元数据....
  • 这不可能吗?我已经在几个网站上发布了这个问题,似乎没有人看到我的问题可以提供一个像样的答案......

标签: python xml tags


【解决方案1】:

我相信我成功地使代码工作了。如果您需要从现有的 XML 文件更改它们,这将允许您编辑某些标签。我需要这样做以在批处理脚本中为某些 GIS shapefile 创建元数据,以根据它们是单个日期、多个日期还是某个日期范围来更改某些日期值。

这个网页帮了大忙:http://lxml.de/tutorial.html

我还有一些工作要做,但这是我从最初的问题中寻找的答案 :) 我确信这可以用于许多其他应用程序。

# Set workspace location for XML files
folderPath = "Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009"
# Loop through each file and search for files with .xml extension
for filename in glob.glob(os.path.join(folderPath, "*.xml")):

    fullpath = os.path.join(folderPath, filename)

    # Split file name from the directory path
    if os.path.isfile(fullpath):
        basename, filename2 = os.path.split(fullpath)
        # Set variable to XML files
        root = ElementTree(file=r"Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009\\" + filename2)

        # Set variable for iterator
        iter = root.getiterator()
        #Iterate through the tags in each XML file
        for element in iter:
            if element.tag == "timeinfo":
                tree = root.find(".//timeinfo")
                # Clear all tags below the "timeinfo" tag
                tree.clear()
                # Append new Element
                element.append(ET.Element("mdattim"))
                # Create SubElements to the parent tag
                child1 = ET.SubElement(tree, "sngdate")
                child2 = ET.SubElement(child1, "caldate")
                child3 = ET.SubElement(child1, "time")
                # Set text values for tags
                child2.text = "08-24-2009"
                child3.text = "unknown

【讨论】:

    猜你喜欢
    • 2011-09-25
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    相关资源
    最近更新 更多