How to send XML request to API with R

I am trying to automate loading part of the 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 lots of information on how to send the xml, but none of the examples is not R-specific .. and there are many R-code that shows how to use XML, httrand RCurl, but I could not find any examples in the SO or r-help mailing list on how to send an xml request ... there is more documentation for parsing the response.

on the government website , if you click on an example PlansForIndividualOrFamily Samples, it will display 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>"
+5
source share
1

RCurl, :

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

. xpathApply XML . , 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"
+9

All Articles