1

I am a bit annoyed that the ENTSO-E only provides XML responses and no JSON formats for their API. Does anyone have experience with converting text/xml responses to dataframes in R? I usually use

    fromJSON(rawToChar(response$content))

from the jsonlite and httr library. So far I never had to convert XML. I tried a few things from the XML library but with no success. All tutorials I found focus on using xml webfiles. In my case I only want the raw content data of the api response to be converted.
The response looks like this:

Response [the.api.I.used] Date: 2022-03-10 17:53 Status: 200 Content-Type: text/xml Size: 1.09 MB

if I use

    content(response, as = "text")

I get this:

enter image description here

So I have something like position and quantity.

if I use this result in

    xmlParse(content(response, as = "text"))

I get this output:

enter image description here

How do I make a dataframe out of it? Is there a function similar to fromJSON for XML? If you need a minimal working code example I can provide one with a public API but for this very example a private token is required.

thanks and best, Johannes

Solution:

response_example = httr::GET(url)
content_example <- httr::content(response_example, encoding = "UTF-8")
content_list_example <- xml2::as_list(content_example)
timeseries_example <- content_list_example$GL_MarketDocument[names(content_list_example$GL_MarketDocument) == "TimeSeries"]  *"GL_MarketDocument" and "TimeSeries" is specific to my API response*
values <- as.numeric(unlist(purrr::map(ts$TimeSeries$Period, "quantity")))
  • I Would suggest the xml2 package. You can search for r and xml2 and find plenty of questions/answers to use as an example. – Dave2e Mar 11 '22 at 02:02
  • Hi, thank you for your suggestion. I solved my problem with the xlm2 library and some tutorials but I have to say it's not intuitive at all. Might be a problem with the entso e database and not with xml handling in general. I will post how I got the values I wanted later. – Johannes Schwenzer Mar 11 '22 at 19:39
  • Hi @JohannesSchwenzer, what was the solution you found? I'm currently struggling with this very problem! – a_leemo May 18 '23 at 03:36
  • 1
    Hi @a_leemo , I updated my question with the solution code. The added functions were xml2::as_list and unlist(purrr::map(data,"Quantity")) – Johannes Schwenzer May 19 '23 at 09:27
  • Thanks @JohannesSchwenzer, I ended up getting a good working solution for day ahead prices, but it's rather a lot less elegant than this. $%#* XML! – a_leemo May 21 '23 at 03:40

0 Answers0