Reputation:
I would like to extract data as dataframes from an XML file available under: http://www.uniprot.org/uniprot/P43405.xml
I only get back empty string although I think that the xpath queries are okay.
library(RCurl)
library(XML)
url <- "http://www.uniprot.org/uniprot/P43405.xml"
urldata <- getURL(url)
xmlfile <- xmlParse(urldata)
# some xpath queries
xmlfile["//entry/comment[@type='function']/text"]
xmlfile["//entry/comment[@type='PTM']/text"]
xpathSApply(xmlfile,"//uniprot/entry",xmlGetAttr, 'dataset')
xpathSApply(xmlfile,"//uniprot/entry",xmlValue)
Can anyone help me with this problem?
Thanks, Frank
Upvotes: 1
Views: 607
Reputation:
Thanks for the help! YEs, the namespaces were missing. I added some additional code. Maybe that will help others to get familiar with XML.
library(RCurl)
library(XML)
url <- "http://www.uniprot.org/uniprot/P43405.xml"
urldata <- getURL(url)
xmlfile <- xmlParse(urldata)
getNodeSet(xmlfile, "//entry//comment")
# one needs the name space here
namespaces <- c(ns="http://uniprot.org/uniprot")
# extract all comments, make a data frame
comments.uniprot <- getNodeSet(xmlfile, "//ns:entry//ns:comment", namespaces)
comments.dataframe <- as.data.frame(sapply(comments.uniprot, xmlValue))
comments.attributes <- as.data.frame(sapply(comments.uniprot, xmlGetAttr,'type'))
comments.all <- cbind(comments.attributes,comments.dataframe)
# only extract PTM comments
PTMs <- getNodeSet(xmlfile, "//ns:entry//ns:comment[@type='PTM']/ns:text", namespaces)
PTMs2 <- sapply(PTMs, xmlValue)
PTMs2.dataframe <- as.data.frame(PTMs2)
xpathSApply(xmlfile,"//ns:uniprot/ns:entry",xmlGetAttr, 'dataset', namespaces=namespaces)
xpathSApply(xmlfile,"//ns:uniprot/ns:entry/ns:accession",xmlValue, namespaces=namespaces)
Upvotes: 0
Reputation: 1754
Namespaces are missing:
library(RCurl)
library(XML)
url <- "http://www.uniprot.org/uniprot/P43405.xml"
urldata <- getURL(url)
xmlfile <- xmlParse(urldata)
getNodeSet(xmlfile, "//entry//comment")
namespaces <- c(ns="http://uniprot.org/uniprot")
getNodeSet(xmlfile, "//ns:entry//ns:comment", namespaces)
getNodeSet(xmlfile, "//ns:entry//ns:comment[@type='PTM']/ns:text", namespaces)
xpathSApply(xmlfile,"//ns:uniprot/ns:entry",xmlGetAttr, 'dataset', namespaces=namespaces)
xpathSApply(xmlfile,"//ns:uniprot/ns:entry",xmlValue, namespaces=namespaces)
References:
?xpathApply
How can I use xpath querying using R's XML library?
Upvotes: 1