Friday, 15 April 2011

xml - Determine how many children a div has -



xml - Determine how many children a div has -

how can determine how many elments div class="pagination" has, using xml?

<div class="pagination"> <span class="current"> 1 </span> <a class="" href="#"> 2 </a> <a class="" href="#"> 3 </a> <a class="" href="#"> 4 </a> </div>

the output should be: 4.

update 1:

i've tried this:

library(xml) urrrl <- "http://www.tagesanzeiger.ch/service/suche/suche.html?order=relevanz&q=ecopop&submitsearch=suchen&date=alle#" download.file(url = urrrl, destfile = "overview.xml") parsed <- htmlparse("overview.xml") nodes <- getnodeset(parsed, "//div[@class='pagination']") nodes list() attr(,"class") [1] "xmlnodeset" aid <- lapply(nodes, function(x) xmlsapply(x, xmlvalue)) aid list()

update 2:

it seems problem somehow related fact there underlying script or sth, enables click through search results "hand". how overcome this? again

you can utilize count() function of xml 1.0. here example:

doc <- htmltreeparse("http://www.cgalaw.com/contact", useinternal = true) (string <- xpathsapply(doc, "count(//h3)", xmlvalue, trim = true)) [1] 5

there 5 nodes in doc h3. think can run like

xpathsapply(doc, "count(div[@class='pagination'])", xmlvalue, trim = true)

xml r

No comments:

Post a Comment