How to deal with xmlns values while parsing an XML file?

后端 未结 1 1880
执笔经年
执笔经年 2020-12-12 06:36

I have the following toy example of an XML file. I have thousands of these. I have difficulty parsing this file.

Look at the text in second line. All my original fi

相关标签:
1条回答
  • 2020-12-12 07:24

    The confusion was caused by the following default namespace (namespace declared without prefix) :

    xmlns="http://schemas.datacontract.org/Storage"
    

    Note that descendants elements without prefix inherit default namespace from ancestor, implicitly. Now, to reference element in namespace, you need to map a prefix to the namespace URI, and use that prefix in your XPath :

    ns = {'d': 'http://schemas.datacontract.org/Storage' }
    val_of_interest = root.findall('./d:Values/d:SensorValue', ns)
    
    for sensor_val in val_of_interest:
        print sensor_val.find('d:accelx', ns).text
        print sensor_val.find('d:accely', ns).text
    
    0 讨论(0)
提交回复
热议问题