posting data using xml with R

2019-06-14 21:19发布

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

标签: r rcurl httr
1条回答
成全新的幸福
2楼-- · 2019-06-14 21:37

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()
查看更多
登录 后发表回答