0

hi I am trying to parse xml with namespace and attribute.

I am almost close by using root.findall() and .get()

However still struggling to get the accurate values from xml file.

How to get the xml attribute values ?

Input:

<?xml version="1.0" encoding="UTF-8"?><message:GenericData 

xmlns:message="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" 
xmlns:common="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:generic="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" 
xsi:schemaLocation="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message https://sdw-
wsrest.ecb.europa.eu:443/vocabulary/sdmx/2_1/SDMXMessage.xsd 
http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common https://sdw-
wsrest.ecb.europa.eu:443/vocabulary/sdmx/2_1/SDMXCommon.xsd 
http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic https://sdw-
wsrest.ecb.europa.eu:443/vocabulary/sdmx/2_1/SDMXDataGeneric.xsd">

<generic:Obs>
<generic:ObsDimension value="1999-01"/>
<generic:ObsValue value="0.7029125"/>
</generic:Obs>

<generic:Obs>
<generic:ObsDimension value="1999-02"/>
<generic:ObsValue value="0.688505"/>
</generic:Obs>

Code:

import xml.etree.ElementTree as ET
tree = ET.parse("file.xml")
root = tree.getroot()
for x in root.findall('.//'):
    print(x.tag, " ", x.get('value'))

Output:

{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}Obs   None
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsDimension   1999-01
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsValue   0.7029125
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}Obs   None
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsDimension   1999-02
{http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic}ObsValue   0.688505

Expected_Output:

1999-01  0.7029125

1999-02  0.688505

anonymus
  • 3
  • 3
  • Almost there, `x.get('value')` is giving you the values, just add some logic to show them as expected. – LMC Sep 02 '22 at 15:05

1 Answers1

0

How about this:

for parent in root:                                                                 
    print('  '.join([child.get('value', "") for child in parent])) 
DraftyHat
  • 428
  • 1
  • 2
  • 6
  • getting error after trying: `TypeError: sequence item 0: expected str instance, NoneType found` – anonymus Sep 02 '22 at 22:18
  • Ah, I see that when one of the children doesn't have a value attribute. I've edited the `get()` command above with a default value of "", that took care of the issue for me. – DraftyHat Sep 02 '22 at 23:25