2011-08-02 9 views
2

これまで同様の質問がありましたが、これは少し異なります。私はPythonを使ってXMLタグを見つけて置き換えたいと思っています。私はいくつかのGISシェープファイルのメタデータとしてXMLをアップロードしています。メタデータエディタでは、特定のデータが収集される日付を選択するオプションがあります。オプションは「単一の日付」、「複数の日付」および「日付の範囲」です。日付の範囲のタグを含む最初のXMLでは、いくつかのサブ要素 'begdate'、 'begtime'、 'enddate'およびタグを含むタグ "rngdates"が表示されます。これらのタグを編集して、複数の単一の日付を含む2番目のXMLのように見せたいと思います。新しいタグは 'mdattim'、 'sngdate'、 'caldate'です。私はこれが十分にはっきりしていることを願っていますが、必要に応じてさらに情報を求めてください。 XMLは変わった獣なので、まだ完全に理解していません。Pythonを使用して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") 
+2

なぜXSLTを使用しないのですか? – GaretJax

+3

また、一方を他方に変換するための規則を示してください。私。入力と、その入力から生成される予想出力を表示します。 –

+0

最初のXMLが入力です。私はいくつかのタグの間にキーワードを埋め込んだテンプレートXMLをいくつか持っています。 2番目は手動で編集した出力です。最初のXMLを編集して、最初のXMLのtimeinfoタグ間のすべてが2番目のXMLの同じタグ間のすべてに置き換えられるようにします。私はPythonを使用しています。これはArcGIS関数であり、Pythonが優先言語なのでです。私はこのスクリプトをPythonツールと併用しています。私のスクリプトは、多数のGISシェープファイルでメタデータとして使用されるXMLをバッチ処理するために使用されます。 – Mike

答えて

1

Iベル私はコードを作るのに成功しました。これにより、既存のXMLファイルからタグを変更する必要がある場合、特定のタグを編集することができます。バッチ処理スクリプトでいくつかのGISシェープファイルのメタデータを作成して、単一の日付、複数の日付、または日付の範囲に応じて特定の日付の値を変更する必要がありました。

このWebページでは、たくさん助け: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 
関連する問題