Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python查找和替换XML中的标记_Python_Xml_Tags - Fatal编程技术网

使用Python查找和替换XML中的标记

使用Python查找和替换XML中的标记,python,xml,tags,Python,Xml,Tags,我以前也提出过类似的问题,但这个问题略有不同。我想使用python查找和替换XML标记。我正在使用XML作为一些GIS形状文件的元数据上传。在元数据编辑器中,我可以选择收集某些数据的日期。选项有“单一日期”、“多个日期”和“日期范围”。在第一个XML中,包含一系列日期的标记,您将看到标记“rngdates”,其中包含一些子元素“begdate”、“begtime”、“enddate”和。我想编辑这些标记,使其看起来像第二个包含多个单一日期的XML。新标签是“mdattim”、“sngdate”和

我以前也提出过类似的问题,但这个问题略有不同。我想使用python查找和替换XML标记。我正在使用XML作为一些GIS形状文件的元数据上传。在元数据编辑器中,我可以选择收集某些数据的日期。选项有“单一日期”、“多个日期”和“日期范围”。在第一个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文件中更改某些标记,这将允许您编辑它们。我需要这样做,以便在批处理脚本中为某些GIS形状文件创建元数据,从而根据日期是单个日期、多个日期还是一系列日期来更改某些日期值

这个网页帮助很大:

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

# 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

另外,向我们展示将一个转换为另一个的规则。例如,显示输入和从该输入生成的预期输出。第一个XML是输入。我有许多模板XML,它们在某些标记之间嵌入了关键字。第二个是我手动编辑的输出。我想编辑第一个XML,这样第一个XML中的timeinfo标记之间的所有内容都将替换为第二个XML中相同标记之间的所有内容。我使用Python是因为这是一个ArcGIS函数,Python是首选语言。我将此脚本与他们的python工具结合使用。我的脚本将用于批处理XML,将其用作大量GIS形状文件中的元数据……这是不可能的吗?我已经在几个网站上发布了这篇文章,似乎没有人能给出一个像样的答案。。。
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")
# 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