我想將XML文件中的特定數據提取到R數據框。我想稍後使用這些數據來重建Anoto筆的數字化筆畫。 到目前爲止,我通過使用庫rvest來做到這一點。 (示例XML文件可以在下面找到)R&XML - 將數據分配給在數據框中同名的正確父節點
library(rvest)
file <- read_xml("1.xml")
#The interesting data is in the stroke nodes.
stroke <- xml_nodes(file, "stroke")
#One example for extracting data I am interested in.
bounds <- xml_nodes(stroke, "bounds")
x <- xml_text(xml_nodes(bounds, "x"))
y <- xml_text(xml_nodes(bounds, "y"))
width <- xml_text(xml_nodes(bounds, "width"))
height <- xml_text(xml_nodes(bounds, "height"))
#Putting this data into a Dataframe.
df <- data.frame(x, y, width, height)
到目前爲止好。我現在的問題是<sample>
節點。在XML文件中,我有一個<stroke>
節點的最小數量,最多可達約。最大100每個<stroke>
節點都有自己的<sample>
節點。我想以一種方式從樣本節點中提取x,y和時間數據,我可以將它們分配給數據框中的相應筆劃。 例如,如果我只是做
mysamples <- xml_nodes(stroke, "sample")
我收到的所有樣本都招,但我有不同的筆觸來區分。 我想過編寫一個使用for循環遍歷不同筆畫的函數,但是我無法完成這個任務。
這是一個簡短的XML文件示例,其中包含兩個<stroke>
節點。
<?xml version="1.0" encoding="UTF-8" ?>
<page>
<UnassignedStrokes>
<starttime>1459867893629</starttime>
<endtime>1459867896812</endtime>
<stroke>
<starttime>1459867893629</starttime>
<endtime>1459867894815</endtime>
<linewidth>1.0</linewidth>
<color>-14090101</color>
<bounds>
<x>260.0</x>
<y>750.0</y>
<width>217.0</width>
<height>18.0</height>
</bounds>
<sample>
<x>260.625</x>
<y>766.0</y>
<time>1459867893629</time>
<force>108</force>
</sample>
<sample>
<x>260.625</x>
<y>763.625</y>
<time>1459867893722</time>
<force>120</force>
</sample>
<sample>
<x>262.875</x>
<y>762.0</y>
<time>1459867893775</time>
<force>122</force>
</sample>
</stroke>
<stroke>
<starttime>1459867895892</starttime>
<endtime>1459867896812</endtime>
<linewidth>1.0</linewidth>
<color>-14090101</color>
<bounds>
<x>364.0</x>
<y>701.0</y>
<width>10.0</width>
<height>125.0</height>
</bounds>
<sample>
<x>364.5</x>
<y>701.0</y>
<time>1459867895892</time>
<force>32</force>
</sample>
<sample>
<x>366.0</x>
<y>702.0</y>
<time>1459867895905</time>
<force>106</force>
</sample>
<sample>
<x>367.25</x>
<y>702.625</y>
<time>1459867895958</time>
<force>120</force>
</sample>
</stroke>
</UnassignedStrokes>
</page>
我高度讚賞任何幫助!
當前您的界限數據框不捕捉筆畫。你是否需要兩個數據框:邊界和樣本以筆劃數據標識爲列?在XML中,兩者都是彼此的兄弟姐妹。請顯示所需的最終結果。 – Parfait