posting data using xml with R

孤街浪徒 提交于 2020-02-23 08:30:06

问题


I want to post xml with R, the code in python is

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" 

now, I want to use R to finish the same function, How to do.

I have tried several times.

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" attr(,"Content-Type") charset "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 the website html code.

# 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\">Error

Error

ErrorThis page can't be displayed. Contact support for additional information.
The incident ID is: 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

result

OK

0

h$value()

[1] ""


I have solved the problem.

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:


I have solved the problem.

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


来源:https://stackoverflow.com/questions/30513616/posting-data-using-xml-with-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!