0

I am using a public API and the response im getting from the API is

<string xmlns="http://ws.sdde.bccr.fi.cr">&lt;Datos_de_INGC011_CAT_INDICADORECONOMIC&gt;
  &lt;INGC011_CAT_INDICADORECONOMIC&gt;
    &lt;COD_INDICADORINTERNO&gt;3148&lt;/COD_INDICADORINTERNO&gt;
    &lt;DES_FECHA&gt;2016-01-01T00:00:00-06:00&lt;/DES_FECHA&gt;
    &lt;NUM_VALOR&gt;533.00000000&lt;/NUM_VALOR&gt;
  &lt;/INGC011_CAT_INDICADORECONOMIC&gt;
  &lt;INGC011_CAT_INDICADORECONOMIC&gt;
    &lt;COD_INDICADORINTERNO&gt;3148&lt;/COD_INDICADORINTERNO&gt;
    &lt;DES_FECHA&gt;2016-01-02T00:00:00-06:00&lt;/DES_FECHA&gt;
    &lt;NUM_VALOR&gt;533.00000000&lt;/NUM_VALOR&gt;
  &lt;/INGC011_CAT_INDICADORECONOMIC&gt;
&lt;/Datos_de_INGC011_CAT_INDICADORECONOMIC&gt;</string>

I am trying to parse that XML using ElementTree but it is not working, my assumption is that its not working beacuse of the &lt and &gt that are in the response instead of <> but even after replacing the xml parser still doesnt work

Any ideas?

The code im using:

response = requests.get(url)
cleaned_data = response.text.replace('&lt;','<')
cleaned_data = cleaned_data.replace('&gt;','>')
tree = ET.fromstring(cleaned_data)
print(tree)
1
  • Don't tell us something "isn't working". Tell us how it failed. Commented Mar 11, 2022 at 8:39

1 Answer 1

1

I couldn't find the reported error, using lib lxml or xml (built-in) with python3.9.

import lxml.etree as ET
from pprint import pprint


xml_str = """
<string xmlns="http://ws.sdde.bccr.fi.cr">&lt;Datos_de_INGC011_CAT_INDICADORECONOMIC&gt;
  &lt;INGC011_CAT_INDICADORECONOMIC&gt;
    &lt;COD_INDICADORINTERNO&gt;3148&lt;/COD_INDICADORINTERNO&gt;
    &lt;DES_FECHA&gt;2016-01-01T00:00:00-06:00&lt;/DES_FECHA&gt;
    &lt;NUM_VALOR&gt;533.00000000&lt;/NUM_VALOR&gt;
  &lt;/INGC011_CAT_INDICADORECONOMIC&gt;
  &lt;INGC011_CAT_INDICADORECONOMIC&gt;
    &lt;COD_INDICADORINTERNO&gt;3148&lt;/COD_INDICADORINTERNO&gt;
    &lt;DES_FECHA&gt;2016-01-02T00:00:00-06:00&lt;/DES_FECHA&gt;
    &lt;NUM_VALOR&gt;533.00000000&lt;/NUM_VALOR&gt;
  &lt;/INGC011_CAT_INDICADORECONOMIC&gt;
&lt;/Datos_de_INGC011_CAT_INDICADORECONOMIC&gt;</string>
"""

cleaned_data = xml_str.replace("&lt;", "<")
cleaned_data = cleaned_data.replace("&gt;", ">").strip()
print("cleaned_data")
pprint(cleaned_data)

tree = ET.fromstring(cleaned_data)
print("\ntree")
print(tree)
pprint(ET.tostring(tree).decode())

Output:

cleaned_data
('<string '
 'xmlns="http://ws.sdde.bccr.fi.cr"><Datos_de_INGC011_CAT_INDICADORECONOMIC>\n'
 '  <INGC011_CAT_INDICADORECONOMIC>\n'
 '    <COD_INDICADORINTERNO>3148</COD_INDICADORINTERNO>\n'
 '    <DES_FECHA>2016-01-01T00:00:00-06:00</DES_FECHA>\n'
 '    <NUM_VALOR>533.00000000</NUM_VALOR>\n'
 '  </INGC011_CAT_INDICADORECONOMIC>\n'
 '  <INGC011_CAT_INDICADORECONOMIC>\n'
 '    <COD_INDICADORINTERNO>3148</COD_INDICADORINTERNO>\n'
 '    <DES_FECHA>2016-01-02T00:00:00-06:00</DES_FECHA>\n'
 '    <NUM_VALOR>533.00000000</NUM_VALOR>\n'
 '  </INGC011_CAT_INDICADORECONOMIC>\n'
 '</Datos_de_INGC011_CAT_INDICADORECONOMIC></string>')

tree
<Element {http://ws.sdde.bccr.fi.cr}string at 0x110446900>
('<string '
 'xmlns="http://ws.sdde.bccr.fi.cr"><Datos_de_INGC011_CAT_INDICADORECONOMIC>\n'
 '  <INGC011_CAT_INDICADORECONOMIC>\n'
 '    <COD_INDICADORINTERNO>3148</COD_INDICADORINTERNO>\n'
 '    <DES_FECHA>2016-01-01T00:00:00-06:00</DES_FECHA>\n'
 '    <NUM_VALOR>533.00000000</NUM_VALOR>\n'
 '  </INGC011_CAT_INDICADORECONOMIC>\n'
 '  <INGC011_CAT_INDICADORECONOMIC>\n'
 '    <COD_INDICADORINTERNO>3148</COD_INDICADORINTERNO>\n'
 '    <DES_FECHA>2016-01-02T00:00:00-06:00</DES_FECHA>\n'
 '    <NUM_VALOR>533.00000000</NUM_VALOR>\n'
 '  </INGC011_CAT_INDICADORECONOMIC>\n'
 '</Datos_de_INGC011_CAT_INDICADORECONOMIC></string>')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.