用于读取LANDSAT XML元文件的Python脚本

时间:2012-11-06 18:04:50

标签: python landsat

我需要获取几张Landsat图像的场景ID。图像的元数据包含在xml文件中:

<?xml version="1.0"?>
<searchResponse xsi:schemaLocation="http://upe.ldcm.usgs.gov/schema/metadata
http://earthexplorer.usgs.gov/EE/metadata.xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
xmlns="http://upe.ldcm.usgs.gov/schema/metadata">
    <metaData>
        <browseAvailable>Y</browseAvailable
        <browseURL>http://earthexplorer.usgs.gov/browse/tm/146/41/1998/LT51460411998300XXX01.jpg</browseURL> 
        <sceneID>LT51460411998300XXX01</sceneID> 
        <sensor>LANDSAT_TM</sensor> 
        <acquisitionDate>1998-10-27</acquisitionDate> 
        <dateUpdated>2012-07-31</dateUpdated> 
        <path>146</path> 
        <row>41</row> 
        <...>
     </metaData>
     <metaData>
        <sceneID>LT51460411998300XXX01</sceneID> 
        <sensor>LANDSAT_TM</sensor> 
        <acquisitionDate>1998-10-27</acquisitionDate> 
        <dateUpdated>2012-07-31</dateUpdated> 
        <path>146</path> 
        <row>41</row> 
        <...>
    <etc etc>

以下代码将列出metaData中的所有节点(browseAvailable,browseURL,sceneID),但我真正想做的只是获取一个sceneID列表。

#!/usr/bin/python

#import os
#import sys
from xml.dom.minidom import parse

import xml.etree.ElementTree as ET
#import grass.script as grass

tree = ET.parse('C:/Users/Simon/Documents/tif/Metadata/metadata_test1.xml')
root = tree.getroot()

for metadata in root:  
    for data in metadata:          
    sceneID = data.text
        grass.message('data -> %s' % (sceneID))

我尝试过使用Python xml.etree.ElementTree上描述的工具但没有成功。任何人都可以帮我调整上面的代码只返回sceneID吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

#!/usr/bin/python

from xml.dom.minidom import parse

dom = parse('C:/Users/Simon/Documents/tif/Metadata/metadata_test1.xml')

ids  = dom.getElementsByTagName('sceneID')
for id in ids:
    print "%s" % id.childNodes[0].nodeValue