2

I'm having a problem writing my parsed html to a file. I have this data from a table specified from xpath but when I try to write it to a file, I get "Error in cat(list(...)).

> fileUrl <- "http://www.w3schools.com/html/html_tables.asp"
> library(XML)
> htmlFile <- htmlTreeParse(fileUrl, useInternal = TRUE)
> # and then I grab the table
> urlParse <- xpathSApply(htmlFile, "//table[@class='reference']")
> urlParse[[1]]
[[1]]
<table class="reference" style="width:100%">
  <tr><th>Number</th>&#13;
    <th>First Name</th>&#13;
    <th>Last Name</th>      &#13;
    <th>Points</th>&#13;
</tr>
  <tr><td>1</td>&#13;
    <td>Eve</td>&#13;
    <td>Jackson</td>        &#13;
    <td>94</td>&#13;
</tr>
  <tr><td>2</td>&#13;
    <td>John</td>&#13;
    <td>Doe</td>        &#13;
    <td>80</td>&#13;
</tr>
  <tr><td>3</td>&#13;
    <td>Adam</td>&#13;
    <td>Johnson</td>        &#13;
    <td>67</td>&#13;
</tr>
  <tr><td>4</td>&#13;
    <td>Jill</td>&#13;
    <td>Smith</td>      &#13;
    <td>50</td>&#13;
</tr>
</table> 

this is fine, but when I write this to a file, I get:

> write(urlParse[[1]], file = "file.txt") 
Error in cat(list(...), file, sep, fill, labels, append) : 
  argument 1 (type 'externalptr') cannot be handled by 'cat'

but when I do something like:

> write(c(3234,234,23,4,234), file = "file.txt") 

everything is fine. Is it because it's a list? I tried urlParse[1], toString(urlParse[1]), urlParse[[1]][1]. Not sure why.

1 Answer 1

2

Your XML is currently represented by C-level objects. You need to convert it to a string. saveXML can be used to do this:

fileUrl <- "http://www.w3schools.com/html/html_tables.asp"
library(XML)
htmlFile <- htmlTreeParse(fileUrl, useInternal = TRUE)
urlParse <- xpathSApply(htmlFile, "//table[@class='reference']")
myXML <- saveXML(urlParse[[1]])
write(myXML, file = "file.txt")

or simply

saveXML(urlParse[[1]], file = "file.txt")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.