I have a XML file with nodes having the same name and I can't find out how to select the data I want in order to create a dataframe. I'm new to R and XML and I can't seem to make it work!
My XML looks like this (only the beginning):
<GL_Document xmlns="">
<time_Period.timeInterval>...</time_Period.timeInterval>
<TimeSeries>
<mRID>1</mRID>
<MktPSRType>
<psrType>ProdType1</psrType>
</MktPSRType>
<Period>
<timeInterval>
<start>2015-12</start>
<end>2016-11</end>
</timeInterval>
<resolution>PT60M</resolution>
<Point>
<position>1</position>
<quantity>9</quantity>
</Point>
<Point>
<position>2</position>
<quantity>7</quantity>
</Point>
<Point>
<position>3</position>
<quantity>9</quantity>
</Point>
The code contains different nodes named "TimeSeries" with the same type of data (here Period is not developed in order to see the whole structure)
<GL_Document xmlns="">
<time_Period.timeInterval>...</time_Period.timeInterval>
<TimeSeries>
<mRID>1</mRID>
<MktPSRType>
<psrType>ProdType1</psrType>
</MktPSRType>
<Period>...</Period>
</TimeSeries>
<TimeSeries>
<mRID>2</mRID>
<MktPSRType>
<psrType>ProdType2</psrType>
</MktPSRType>
<Period>...</Period>
</TimeSeries>
<TimeSeries>...</TimeSeries>
<TimeSeries>...</TimeSeries>
<TimeSeries>...</TimeSeries>
<TimeSeries>...</TimeSeries>
<TimeSeries>...</TimeSeries>
</GL_Document>
I want to get the data in the following format:
psrType position quantity
ProdType1 1 9
... ... ...
ProdType2 1 ...
I have tried to apply the solutions as mentioned in this post: How to parse XML to R data frame But unsuccessfully. My code looks like this, but I the resulting dataframe contains no data:
xml.url3<-getURL(xml.file3)
doc<-xmlParse(xml.url3)
position_path<-"//GL_Document/TimeSeries/Period/Point/position"
quantity_path<-"//GL_Document/TimerSeries/Period/Point/quantity"
df<-data.frame(
pos=as.integer(sapply(doc[position_path],as,"integer")),
quant=as.integer(sapply(doc[quantity_path],as,"integer")))
Thank you very much for your help!