Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

how to send an XML request to an API with R

i am trying to automate the download of a bit of data from the API of this government website.

the website instructs:

All requests for this version should be made to the following URL:
http://api.finder.healthcare.gov/v2.0/

i can find plenty of information about how to send xml requests, but none of the examples are R-specific.. and there's plenty of R code out there that shows how to use the XML, httr, and RCurl packages, but i couldn't find any examples on SO or the r-help mailing list of how to actually send an xml request. ..there's more documentation for parsing the response.

on the government website, if you click the PlansForIndividualOrFamily Samples example, it displays the xml request (code below) that needs to be sent.

url <- "http://api.finder.healthcare.gov/v2.0/"

xml.request <-
    "<?xml version='1.0' encoding='UTF-8'?>
    <PrivateOptionsAPIRequest>
        <PlansForIndividualOrFamilyRequest>
            <Enrollees>
                <Primary>
                    <DateOfBirth>1990-01-01</DateOfBirth>
                    <Gender>Male</Gender>
                    <TobaccoUser>Smoker</TobaccoUser>
                </Primary>
            </Enrollees>
            <Location>
                <ZipCode>69201</ZipCode>
                <County>
                    <CountyName>CHERRY</CountyName>
                    <StateCode>NE</StateCode>
                </County>
            </Location>
            <InsuranceEffectiveDate>2012-10-01</InsuranceEffectiveDate>
        <IsFilterAnalysisRequiredIndicator>false</IsFilterAnalysisRequiredIndicator>
        <PaginationInformation>
            <PageNumber>1</PageNumber>
            <PageSize>10</PageSize>
        </PaginationInformation>
        <SortOrder>
            <SortField>OOP LIMIT - INDIVIDUAL - IN NETWORK</SortField>
            <SortDirection>ASC</SortDirection>
        </SortOrder>
        <Filter/>
        </PlansForIndividualOrFamilyRequest>
    </PrivateOptionsAPIRequest>"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using RCurl you do this ;

myheader=c(Connection="close", 
            'Content-Type' = "application/xml",
             'Content-length' =nchar(xml.request))
data =  getURL(url = url,
                    postfields=xml.request,
                    httpheader=myheader,
                    verbose=TRUE)

That's about it. Then You can using xpathApply of XML package to retrieve data. For example to get families ID:

library(XML)
xmltext  <- xmlTreeParse(data, asText = TRUE,useInternalNodes=T)
 unlist(xpathApply(xmltext,'//Plan/PlanID',xmlValue))  ## change the right xpath here

 "29678NE0780012" "29678NE0780011" "29678NE0140010"  
  "29678NE0780010" "29678NE0140019" "29678NE0140018" "29678NE0140017"
  "29678NE0140016" "29678NE0780005" "29678NE0780004"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...