title: Pulling Google Finance portfolios into R
body: <p>I want to access data from my google finance portfolio in R.  I am trying to do this by reading the <a href="http://code.google.com/apis/finance/docs/2.0/developers_guide_protocol.html" rel="noreferrer">Google Finance API</a> documentation and following the lead of <a href="http://www.omegahat.org/RGoogleDocs/" rel="noreferrer">RGoogleDocs</a>.  I've made some progress, but I'm having a lot of trouble parsing the XML version of a google finance portfolio.</p>
  
  <pre><code>require(RGoogleDocs)
  
  #Autheticate using RGoogleDocs
  auth = getGoogleAuth("me@gmail.com", "password",service="finance")
  con = getGoogleDocsConnection(auth)
  
  #Get positions
  positions &lt;- getURL("http://finance.google.com/finance/feeds/default/portfolios/6/positions",curl=con)
  positions &lt;- xmlInternalTreeParse(positions)
  positions['entry'] #Returns nothing, should return a list of stocks
  xpathApply(positions, "//a") #Also returns nothing
  </code></pre>
  
  <p>Here is an example.  I am trying to parse all the 'entry' elements out of this document.</p>
  
  <pre><code>require(XML)
  positions &lt;-  "&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;entry xmlns='http://www.w3.org/2005/Atom' xmlns:gf='http://schemas.google.com/finance/2007' xmlns:gd='http://schemas.google.com/g/2005'&gt;&lt;id&gt;http://finance.google.com/finance/feeds/me@gmail.com/portfolios/7/positions/NYSE:SPY&lt;/id&gt;&lt;updated&gt;2011-07-18T21:42:07.000Z&lt;/updated&gt;&lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/finance/2007#position'/&gt;&lt;title type='text'&gt;SPDR S&amp;amp;P 500 ETF&lt;/title&gt;&lt;link rel='self' type='application/atom+xml' href='http://finance.google.com/finance/feeds/default/portfolios/7/positions/NYSE%3ASPY'/&gt;&lt;gd:feedLink href='http://finance.google.com/finance/feeds/me@gmail.com/portfolios/7/positions/NYSE:SPY/transactions'/&gt;&lt;gf:positionData gainPercentage='0.0' return1w='0.0' return1y='0.0' return3m='0.0' return3y='0.0' return4w='0.0' return5y='0.0' returnOverall='0.0' returnYTD='0.0' shares='0.0'/&gt;&lt;gf:symbol exchange='NYSE' fullName='SPDR S&amp;amp;P 500 ETF' symbol='SPY'/&gt;&lt;/entry&gt;"
  positions &lt;- xmlInternalTreeParse(positions)
  positions['entry']
  </code></pre>
  
  <hr>
  
  <h2>EDIT:</h2>
  
  <p>For some reason the example XML string I posted doesn't quite work the same as a live connection to google docs.  The only way you'll be able to reproduce this code is by setting up a portfolio on google finance.  Make sure to note the ID of your portfolio.  In my example I use portfolio ID 6 (<code>finance/feeds/default/portfolios/6/positions</code>), but yours may be different.</p>
  
  <pre><code>#Autheticate using RGoogleDocs
  require(RGoogleDocs)
  auth = getGoogleAuth("me@gmail.com", "password",service="finance")
  con = getGoogleDocsConnection(auth)
  
  #Get positions
  positions &lt;- getURL("http://finance.google.com/finance/feeds/default/portfolios/6/positions",curl=con)
  doc = xmlTreeParse(positions, useInternalNodes = TRUE)
  kids = xmlChildren(doc, addFinalizer = NA)
  entries &lt;- sapply(kids, "[", "entry")
  entries[1]
  </code></pre>