Home > Net >  How can I retrieve specific information from a XML file using python?
How can I retrieve specific information from a XML file using python?

Time:02-15

I am working with Sentinel-2 Images, and I want to retrieve the Cloud_Coverage_Assessment from the XML file. I need to do this with Python.

Does anyone have any idea how to do this? I think I have to use the xml.etree.ElementTree but I'm not sure how?

The XML file:

<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  ...
  </n1:General_Info>
  <n1:Geometric_Info>
  ...
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  ...
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    ...
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>
    ...
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>

CodePudding user response:

read xml from file

import xml.etree.ElementTree as ET
tree = ET.parse('sentinel2.xml')
root = tree.getroot()

print(root.find('.//Cloud_Coverage_Assessment').text)

CodePudding user response:

..and I want to retrieve the Cloud_Coverage_Assessment

Try the below (use xpath)

import xml.etree.ElementTree as ET


xml = '''<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  </n1:General_Info>
  <n1:Geometric_Info>
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>    
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>'''


root = ET.fromstring(xml)
print(root.find('.//Cloud_Coverage_Assessment').text)

output

90.7287
  • Related