使用 xml 和 R 发布数据

posting data using xml with R

我想postxml用R,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

"Problem creating Query from XML: Content is not allowed in prolog.\nxml=\n\n B0907\n org.pdb.query.simple.ExpTypeQuery\n Experimental Method Search : Experimental Method=SOLID-STATE NMR\n SOLID-STATE NMR\n\n\n" 属性(,"Content-Type") 字符集 "text/plain" "ISO-8859-1"

# 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

return 网站 html 代码。

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

"!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">http://www .w3.org/1999/xhtml\">错误

错误

错误此页面无法显示。联系支持以获取更多信息。
事件 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]“”


我已经解决了问题

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()

我已经解决了问题

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()