无法在R中使用SSOAP的Web服务功能

时间:2014-10-08 19:06:33

标签: r web-services soap

我正试图通过他们的网络服务平台从巴西立法机构检索数据,但我已经陷入困境。虽然我能够将功能分配到R的全局环境中,但我无法使用它们。

这是我到目前为止所得到的:

library(XML)
library(RCurl)
library(XMLSchema)
library(SSOAP)

url_wsdl = "http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx?WSDL"
wsdl <- getURL(url_wsdl, verbose = TRUE, ssl.verifypeer = FALSE, useragent = "R") 
doc = xmlParse(wsdl, asText = TRUE)
def <- processWSDL(doc)
ff  <- genSOAPClientInterface(def = def)

test <- ff@functions$ListarSiglasTipoProposicao()

并生成

Error: 1: Space required after the Public Identifier
2: SystemLiteral " or ' expected
3: SYSTEM or PUBLIC, the URI is missing

点击此链接后点击“调用”后应该生成内容:http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx?op=ListarSiglasTipoProposicao

有没有人对这里出了什么问题有任何建议?

1 个答案:

答案 0 :(得分:3)

您需要用户代理,否则该网站拒绝该请求:

library(XML)
library(RCurl)
library(XMLSchema)
library(SSOAP)

url_wsdl = "http://www.camara.gov.br/SitCamaraWS/Proposicoes.asmx?WSDL"
wsdl <- getURL(url_wsdl, verbose = TRUE, ssl.verifypeer = FALSE, useragent = "R") 
doc = xmlParse(wsdl, asText = TRUE)
def <- processWSDL(doc)
ff  <- genSOAPClientInterface(def = def)

myAgent <- "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"
.opts = list(useragent = myAgent)
test <- ff@functions$ListarSiglasTipoProposicao(.opts = .opts
                                                , .convert = function(x){x})


> test
<ListarSiglasTipoProposicaoResult>
  <siglas xmlns="">
  <sigla tipoSigla="AA        " descricao="Autógrafo" ativa="True" genero="o"/>
  <sigla tipoSigla="ADD       " descricao="Adendo" ativa="True" genero="o"/>
  <sigla tipoSigla="ANEXO     " descricao="Anexo" ativa="True" genero="o"/>
  .......

> str(test)
Classes 'XMLInternalElementNode', 'XMLInternalNode', 'XMLAbstractNode' <externalptr> 

这里我们规定了一个非常简单的操作内容的功能。我们可以提供更好的转换函数来获得更可爱的结果:

test <- ff@functions$ListarSiglasTipoProposicao(.opts = .opts
            , .convert = function(x){
              out <- do.call(rbind, lapply(getNodeSet(x, "//sigla"), xmlAttrs))
              as.data.frame(out, stringsAsFactors = FALSE)
              })
> head(test)
tipoSigla         descricao ativa genero
1 AA                Autógrafo  True      o
2 ADD                   Adendo  True      o
3 ANEXO                  Anexo  True      o
4 APJ              Anteprojeto  True      o
5 ATC        Ato Convocatório  True      o
6 ATOP       Ato do Presidente False      o

编码很棘手,因为xmlAttrs没有与xmlValue不同的编码参数,但你可以尝试以下(跨平台它可能无法工作我只有一个Windows框来进行当前测试):< / p>

myFunc <- function(x){
  input <- SSOAP:::parseSOAP(x$content)
  out <- lapply(getNodeSet(input, "//sigla"), function(x){iconv(xmlAttrs(x), "UTF-8", "UTF-8")})
  out <- do.call(rbind, out)
  as.data.frame(out, stringsAsFactors = FALSE)
}
class(myFunc) <- "RawSOAPConverter"
test <- ff@functions$ListarSiglasTipoProposicao(.opts = .opts, .convert = myFunc)

> head(test)
tipoSigla         descricao ativa genero
1 AA                 Autógrafo  True      o
2 ADD                   Adendo  True      o
3 ANEXO                  Anexo  True      o
4 APJ              Anteprojeto  True      o
5 ATC         Ato Convocatório  True      o
6 ATOP       Ato do Presidente False      o