Skip to content

XML package example code?

7 messages · Charlie Sharpsteen, Tony, Peng Yu +1 more

#
I'm interested in parsing an html page. I should use XML, right? Could
you somebody show me some example code? Is there a tutorial for this
package?
#
Peng Yu wrote:
Did you try looking through the help pages for the XML package or browsing
the Omegahat website?

Look at:

  library(XML)
  ?htmlTreeParse

And the relevant web page for documentation and examples is:

  http://www.omegahat.org/RSXML/


-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
#
Cls59 is correct that there is a lot of example code, just look in ?
htmlTreeParse and you'll get most of what you need i think.

here's some simplified code I use a lot of (XPath expressions are used
to parse the code):

# libraries
library(RCurl)
library(XML)

# google url
my.url <- "http://www.google.co.uk/search?hl=en&client=firefox-
a&rls=org.mozilla%3Aen-GB%3Aofficial&hs=6Sd&q=google
+wave&btnG=Search&meta=&aq=f&oq="

# download page
html <- getURL(my.url)
html.tree <- htmlTreeParse(html, useInternalNodes = TRUE, error =
function(...){})

# the xpath expression is next
nodes <- getNodeSet(html.tree, "//a[@href][@class='l']")
links <- sapply(nodes, function(x) x <- xmlAttrs(x)[[1]])


HTH
Tony
On 25 Nov, 01:49, Peng Yu <pengyu... at gmail.com> wrote:
#
On Wed, Nov 25, 2009 at 12:19 AM, cls59 <chuck at sharpsteen.net> wrote:
http://www.omegahat.org/RSXML/shortIntro.html

I'm trying the example on the above webpage. But I'm not sure why I
got the following error. Would you help to take a look?


$ Rscript main.R
trying URL 'http://www.omegahat.org/RSXML/index.html'
Content type 'text/html; charset=ISO-8859-1' length 3021 bytes
opened URL
==================================================
downloaded 3021 bytes
Opening and ending tag mismatch: dd line 68 and dl
Opening and ending tag mismatch: li line 67 and body
Opening and ending tag mismatch: dt line 66 and html
Premature end of data in tag dd line 64
Premature end of data in tag li line 63
Premature end of data in tag dt line 62
Premature end of data in tag dl line 61
Premature end of data in tag body line 5
Premature end of data in tag html line 1
Error: 1: Opening and ending tag mismatch: dd line 68 and dl
2: Opening and ending tag mismatch: li line 67 and body
3: Opening and ending tag mismatch: dt line 66 and html
4: Premature end of data in tag dd line 64
5: Premature end of data in tag li line 63
6: Premature end of data in tag dt line 62
7: Premature end of data in tag dl line 61
8: Premature end of data in tag body line 5
9: Premature end of data in tag html line 1
Execution halted
#
It's been a long time since i read the tutorials, but 'I think', the
reason you get those notifications is because the html code is
malformed, meaning that some of the opening tags '<dd>' don't have
corresponding end tags </dd> etc.

The XML package seems rather good at working with malformed code, and
therefore I usually just force those notifications into an empty
function.


library(RCurl)
library(XML)
html <- getURL("http://www.omegahat.org/RSXML/index.html")
html.tree <- htmlTreeParse(html, useInternalNodes = TRUE, error =
function(...){})

HTH,
Tony Breyal
On 25 Nov, 16:21, Peng Yu <pengyu... at gmail.com> wrote:
#
Not sure if my code was attached in that last post:

library(RCurl)
library(XML)
html <- getURL("http://www.omegahat.org/RSXML/index.html")
html.tree <- htmlTreeParse(html, useInternalNodes = TRUE, error =
function(...){})
On 25 Nov, 16:21, Peng Yu <pengyu... at gmail.com> wrote:
#
Peng Yu wrote:
You are trying to parse an HTML document as if it were XML.
But HTML is often not well-formed.  So use htmlParse()
for a more forgiving parser.

Or use the RTidyHTML package (www.omegahat.org/RTidyHTML)
to make the HTML well-formed before passing it to xmlTreeParse()
(aka xmlInternalTreeParse()). That package is an interface to
libtidy.

 D.