xml - R xmlParse() Indexing -
i have data generated xmlparse()
. able obtain reference xmlnode called root
> class(root) [1] "xmlinternalelementnode" "xmlinternalnode" "xmlabstractnode"
when, do
> root[[2]][[1]] <tr class="party-republican race-winner"><th rowspan="5" class="results-county">autauga <span class="precincts-reporting">100.0% reporting</span></th> <th scope="row" class="results-candidate">m. romney</th> <td class="results-party"><abbr title="republican">gop</abbr></td> <td class="results-percentage">72.6%</td> <td class="results-popular"> 17,366</td> </tr>
i try reference value in side tag:
<td class="results-percentage">
however doing root[[1]][[2]][["<td class='results-percentage'>]]
returns null
what doing wrong prevents me accessing 72.6% value?
you should give valid xpath
, :
//td[@class='results-percentage'] ## preeceeding td , cotaining class
using data:
library(xml) dd <- xmlparse('<tr class="party-republican race-winner"><th rowspan="5" class="results-county">autauga <span class="precincts-reporting">100.0% reporting</span></th> <th scope="row" class="results-candidate">m. romney</th> <td class="results-party"><abbr title="republican">gop</abbr></td> <td class="results-percentage">72.6%</td> <td class="results-popular"> 17,366</td> </tr> ',astext=true)
then applying xpath
getnodeset(dd, "//td[@class='results-percentage']/text()")[[1]] 72.6%
or using xpathsapply
:
xpathsapply(dd, "//td[@class='results-percentage']",xmlvalue) [1] "72.6%"
Comments
Post a Comment