Parsing Google Sheets data with PHP’s SimpleXML

SimpleXMLAKA: Sheets API access with PHP part 3

Continuing my series on using PHP to manipulate Google Spreadsheets, I’ve added a section to my code that reads the XML representation of a spreadsheet and then parses out the rows and columns.  Note that whenever you’re working with list-based feeds, we’re assuming that the first row contains column headings.

Using SimpleXML, it wasn’t difficult to parse out the cell data.  The biggest hurdle was learning about XML namespaces — at first, I thought that the XML had tags like “gd:etag” or “gsx:quantity” and had to learn that gd and gsx were namespaces.  In SimpleXML, several of the methods take a namespace as a parameter.  You can specify the prefix or namespace URL as described in the doc for the children method here.

I think it’s also possible to get the spreadsheet data as json, but I haven’t tried that yet.

2 thoughts on “Parsing Google Sheets data with PHP’s SimpleXML

  1. Stan

    Hey I don’t understand the namespace part. What is gsx in foreach ($entry->children(‘gsx’, TRUE) as $column) { ???

  2. karl.kranich Post author

    XML Namespaces are ways to make sure that element names don’t conflict with each other. I’m not an expert, and used trial and error to get the PHP tools to be able to “see” the parts of the XML that I wanted to extract or set.

    Karl

Leave a Reply

Your email address will not be published. Required fields are marked *