title: Using PHP to manage GMail Mail Filter XML Files
body: <p>Since GMail made it possible to import and export the mail filters, I'd like to manage the XML files that are exported from GMail using a PHP script, as there are some <a href="http://www.google.com/support/forum/p/gmail/thread?tid=0db8311e7d8d87e0&amp;hl=en" rel="nofollow">known issues</a> with the number of characters in the search filters.</p>
  
  <p>I've found the simplexml_load_file function in PHP, and have performed var_dump() against the performed export, but I now don't seem to be able to access the apps namespace within the generated XML file.</p>
  
  <p>Following various pages within the PHP manual, I created this very simple script to read the XML out so I can start to process out the filters I've already created. Unfortunately, it seems to be missing the key parts!</p>
  
  <pre><code>&lt;pre&gt;&lt;?php
  
  if(file_exists("mailfilters.xml")) {
    $xml = simplexml_load_file("mailfilters.xml");
    $namespaces = $xml-&gt;getNamespaces(true);
    foreach ($namespaces as $prefix =&gt; $ns) {
        $xml-&gt;registerXPathNamespace($prefix, $ns);
    }
    var_dump($xml);
  } else {
    die("Failed to open filter file");
  }
  
  ?&gt;&lt;/pre&gt;
  </code></pre>
  
  <p>This returns this data (extract)</p>
  
  <pre><code>["entry"]=&gt;
  array(268) {
    [0]=&gt;
    object(SimpleXMLElement)#3 (5) {
      ["category"]=&gt;
      object(SimpleXMLElement)#271 (1) {
        ["@attributes"]=&gt;
        array(1) {
          ["term"]=&gt;
          string(6) "filter"
        }
      }
      ["title"]=&gt;
      string(11) "Mail Filter"
      ["id"]=&gt;
      string(45) "tag:mail.google.com,2008:filter:1284991916868"
      ["updated"]=&gt;
      string(20) "2010-10-28T11:59:31Z"
      ["content"]=&gt;
      object(SimpleXMLElement)#272 (0) {
      }
    }
    [1]=&gt;
    object(SimpleXMLElement)#4 (5) {
      ["category"]=&gt;
      object(SimpleXMLElement)#272 (1) {
        ["@attributes"]=&gt;
        array(1) {
          ["term"]=&gt;
          string(6) "filter"
        }
      }
      ["title"]=&gt;
      string(11) "Mail Filter"
      ["id"]=&gt;
      string(45) "tag:mail.google.com,2008:filter:1284991925003"
      ["updated"]=&gt;
      string(20) "2010-10-28T11:59:31Z"
      ["content"]=&gt;
      object(SimpleXMLElement)#271 (0) {
      }
    }
  </code></pre>
  
  <p>Here's an extract from the XML file I have downloaded today, used to create the above output:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'&gt;
      &lt;title&gt;Mail Filters&lt;/title&gt;
  &lt;id&gt;tag:mail.google.com,2008:filters:1284991916868,...,1287734777820&lt;/id&gt;
  &lt;updated&gt;2010-10-28T11:59:31Z&lt;/updated&gt;
  &lt;author&gt;
      &lt;name&gt;My Name&lt;/name&gt;
      &lt;email&gt;my@email.addr.es&lt;/email&gt;
  &lt;/author&gt;
  &lt;entry&gt;
      &lt;category term='filter'&gt;&lt;/category&gt;
      &lt;title&gt;Mail Filter&lt;/title&gt;
      &lt;id&gt;tag:mail.google.com,2008:filter:1284991916868&lt;/id&gt;
      &lt;updated&gt;2010-10-28T11:59:31Z&lt;/updated&gt;
      &lt;content&gt;&lt;/content&gt;
      &lt;apps:property name='from' value='an@email.addr.es'/&gt;
      &lt;apps:property name='shouldArchive' value='true'/&gt;
      &lt;apps:property name='shouldTrash' value='true'/&gt;
  &lt;/entry&gt;
  &lt;entry&gt;
      &lt;category term='filter'&gt;&lt;/category&gt;
      &lt;title&gt;Mail Filter&lt;/title&gt;
      &lt;id&gt;tag:mail.google.com,2008:filter:1284993579743&lt;/id&gt;
      &lt;updated&gt;2010-10-28T11:59:31Z&lt;/updated&gt;
      &lt;content&gt;&lt;/content&gt;
      &lt;apps:property name='subject' value='Some Relevant Subject'/&gt;
      &lt;apps:property name='label' value='MyCoolLabel'/&gt;
      &lt;apps:property name='shouldArchive' value='true'/&gt;
  &lt;/entry&gt;
  </code></pre>
  
  <p>Other "apps:property" verbs include:</p>
  
  <pre><code>&lt;apps:property name='hasTheWord' value=''/&gt;
  &lt;apps:property name='shouldAlwaysMarkAsImportant' value=''/&gt;
  &lt;apps:property name='doesNotHaveTheWord' value=''/&gt;
  </code></pre>