Encoding problems with R XML via SPARQL

痴心易碎 提交于 2019-12-05 12:57:54

instead of SPARQL.R in this case I would consider something like the following: ...

Sys.setenv(LANG="ru")
library(RCurl)
library(XML)
url=<SPARQL endpoint query URL>
xquery=<your query which may contain windows-1251 chars on Windows 7+>
#xquery=iconv(xquery,"CP1251","UTF-8") #it may be required on Windows 7+
param="query"
extrastr=""
resp <- getURL(url = paste(url, '?', param, '=', gsub('\\+','%2B',URLencode(xquery,reserved=TRUE)), extrastr, sep=""), httpheader = c(Accept="application/sparql-results+xml"),.encoding="UTF-8")
xmlResp=xmlParse(resp)
xmlRespRoot=xmlRoot(xmlResp)
nRows=xmlSize(xmlRespRoot[2]$results)
if (nRows>0){
    xRespList=NULL
    for(i in 1:nRows){
    xrow=NULL
    tmprow=(xmlRespRoot[2]$results)[i]$result
    nCols=xmlSize(tmprow)
    for(j in 1:nCols){
        xrow=cbind(xrow,xmlValue(tmprow[j]$binding,encoding="UTF-8"))
    }
    xRespList=rbind(xRespList,xrow)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!