Skip to content
Advertisement

Converting XML to a dataframe R and extracting value from within

I have the following XML which I am trying to convert into a data frame in R or a table within SQL:

I’ve tried using XMLtoDataFrame (“xml” library) but this just creates a table without any values. Would anyone be able to help with this please? Open to a SQL solution.

Edit: I’m looking for my table to be in the following format table

Advertisement

Answer

Here’s an effort.

Assumptions:

  • Root is always there, and always just one
  • Product can contain zero or more, so we’ll iterate smartly and combine
  • I do not assume that $Product$Requests are always perfectly aligned; it can be simplified (hard-coded fewer lines of code) if this is a guaranteed format.

Some helper functions:

Code:

(It’s a “clean” data.frame🙂

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement