Error: XML content does not seem to be XML

I am trying to execute the following lines of code:

library(XML)
url <--"http://www.w3schools.com/xml/simple.xml"
doc <- xmlTreeParse(url, useInternal=TRUE)

But I am getting this error message.

@michaellee
Okay so I’m not going deep into the technicality of the ERROR just to make you understand whats going on.

The error you’re getting is because You’re directly parsing the url which is not even xml
url <--"http://www.w3schools.com/xml/simple.xml"

You’re Parsing “http://www.w3schools.com/xml/simple.xml” (Imagine how can this string be treated as xml)

You need to make a get request which saves the response in a string with a read object to it, You can then easily parse it with the xml parser. Try this code.

library(httr)
library(XML)
url <--"http://www.w3schools.com/xml/simple.xml"
r = GET(url)
doc <- xmlTreeParse(r, useInternal=TRUE)

You will get what you’re looking for even if you don’t specify the encoding.

1 Like