使用带有R的xml发布数据

时间:2015-05-28 17:44:49

标签: r rcurl httr

我想用R发布xml,python中的代码是

import urllib2

url = 'http://www.rcsb.org/pdb/rest/search'

queryText = """
<?xml version="1.0" encoding="UTF-8"?>
<orgPdbQuery>
<version>B0907</version>
<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
<description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>
<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
</orgPdbQuery>
"""

print "query:\n", queryText
print "querying PDB...\n"
req = urllib2.Request(url, data=queryText)
f = urllib2.urlopen(req)
result = f.read()

if result:
    print "Found number of PDB entries:", result.count('\n')
else:
    print "Failed to retrieve results" 

现在,我想用R来完成相同的功能,怎么做。

我已多次尝试过。

library(RCurl)
library(httr)
library(XML)

url1 <- 'http://www.rcsb.org/pdb/rest/search'

xml_text <- '<?xml version="1.0" encoding="UTF-8"?>

<orgPdbQuery>

<version>B0907</version>

<queryType>org.pdb.query.simple.ExpTypeQuery</queryType>

<description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>

<mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>

</orgPdbQuery>'

# first try ----
xml_txt <- xmlTreeParse(xml_text,useInternalNodes=T)
postForm(url1, "xml"=saveXML(xml_txt), style="post")
#failed

&#34;从XML创建查询时出现问题:prolog中不允许使用内容。\ n \ nnml = \ n \ n B0907 \ n org.pdb.query.simple.ExpTypeQuery \ n实验方法搜索:实验方法= SOLID-国家核磁共振\ n固态核磁共振\ n \ n \ n&#34; ATTR(&#34;内容类型&#34)                   字符集 &#34; text / plain的&#34; &#34; ISO-8859-1&#34;

# second try ----
xml_out <- 'tmp.xml'
saveXML(xml_txt, xml_out)
result <- POST(url1, body = list(x = upload_file(xml_out)), encode = 'multipart', )
content(result)
# failed

返回网站的HTML代码。

# 3rd try ----
httpPOST(url1, content = xml_txt)
# failed

&#34;!DOCTYPE html PUBLIC \&#34; - // W3C // DTD XHTML 1.0 Transitional // EN \&#34; \&#34; http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd \&#34;&GT; HTTP://www.w3.org/1999/xhtml \&#34;&GT;错误

错误

错误此页面无法显示。有关其他信息,请联系支持部门。
事故ID为:N / A.
# 4th try ----
h = basicTextGatherer()
result <- curlPerform(url = url1,
  httpheader=c(Accept="text/xml", Accept="multipart/*",
    'Content-Type' = "text/xml; charset=utf-8"),
  postfields=xml_text,
  writefunction = h$update,
  verbose = TRUE
)
result
h$value

# failed

结果

0

H $值()

[1]&#34;&#34;

我已经解决了这个问题。

url1 <- 'http://www.rcsb.org/pdb/rest/search'

xml_text <- '<?xml version="1.0" encoding="UTF-8"?>
  <orgPdbQuery>
  <version>B0907</version>
  <queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
  <description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>
  <mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
  </orgPdbQuery>'
h = basicTextGatherer()
httpheader=c(Accept="*/*",
  "Content-Type"="application/x-www-form-urlencoded")

result <- curlPerform(url = url1,
  httpheader=httpheader,
  postfields=xml_text,
  writefunction = h$update,
  verbose = TRUE
)
result
h$value()

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题。

url1 <- 'http://www.rcsb.org/pdb/rest/search'

xml_text <- '<?xml version="1.0" encoding="UTF-8"?>
  <orgPdbQuery>
  <version>B0907</version>
  <queryType>org.pdb.query.simple.ExpTypeQuery</queryType>
  <description>Experimental Method Search : Experimental Method=SOLID-STATE NMR</description>
  <mvStructure.expMethod.value>SOLID-STATE NMR</mvStructure.expMethod.value>
  </orgPdbQuery>'
h = basicTextGatherer()
httpheader=c(Accept="*/*",
  "Content-Type"="application/x-www-form-urlencoded")

result <- curlPerform(url = url1,
  httpheader=httpheader,
  postfields=xml_text,
  writefunction = h$update,
  verbose = TRUE
)
result
h$value()