我有一個節點具有相同名稱的XML文件,我找不到如何選擇我想要創建數據幀的數據。我是R和XML的新手,我似乎無法完成它!當多個節點具有相同名稱時將XML解析爲R
我的XML看起來是這樣的(僅僅是個開始):
<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>
的代碼包含一個名爲「TimeSeries的」與同類型的數據(這裏週期不同的節點不是爲了看整個結構開發)
<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>
我想要得到的數據格式如下:
psrType position quantity
ProdType1 1 9
... ... ...
ProdType2 1 ...
我試圖應用本帖中提到的解決方案:How to parse XML to R data frame 但是不成功。 我的代碼看起來是這樣,但我得到的數據幀不包含任何數據:
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")))
非常感謝您的幫助!
好了,終於這是我做過什麼: 'XML。 fileexp < - 「https://website.com」 xml.urlexp <-getURL(xml.fileexp) DOC <-xmlParse(xml.urlexp) DF < - ldply(xmlToList(DOC),data.frame) dfexp <-df [df $ .id ==「TimeSeries」,]' – Daldal
好的,對不起,我必須評論兩次..沒有時間留下編輯!然後,我列出了我的時間序列節點內的所有內容(按行)的數據框。由於開始和停止時間已經給出,我可以寫一個代碼,考慮到間隔時間的位置數。可能不是最聰明的解決方案,但它的工作原理! – Daldal