views:

44

answers:

1

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 known issues with the number of characters in the search filters.

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.

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!

<pre><?php

if(file_exists("mailfilters.xml")) {
  $xml = simplexml_load_file("mailfilters.xml");
  $namespaces = $xml->getNamespaces(true);
  foreach ($namespaces as $prefix => $ns) {
      $xml->registerXPathNamespace($prefix, $ns);
  }
  var_dump($xml);
} else {
  die("Failed to open filter file");
}

?></pre>

This returns this data (extract)

["entry"]=>
array(268) {
  [0]=>
  object(SimpleXMLElement)#3 (5) {
    ["category"]=>
    object(SimpleXMLElement)#271 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991916868"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#272 (0) {
    }
  }
  [1]=>
  object(SimpleXMLElement)#4 (5) {
    ["category"]=>
    object(SimpleXMLElement)#272 (1) {
      ["@attributes"]=>
      array(1) {
        ["term"]=>
        string(6) "filter"
      }
    }
    ["title"]=>
    string(11) "Mail Filter"
    ["id"]=>
    string(45) "tag:mail.google.com,2008:filter:1284991925003"
    ["updated"]=>
    string(20) "2010-10-28T11:59:31Z"
    ["content"]=>
    object(SimpleXMLElement)#271 (0) {
    }
  }

Here's an extract from the XML file I have downloaded today, used to create the above output:

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'&gt;
    <title>Mail Filters</title>
<id>tag:mail.google.com,2008:filters:1284991916868,...,1287734777820</id>
<updated>2010-10-28T11:59:31Z</updated>
<author>
    <name>My Name</name>
    <email>[email protected]</email>
</author>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284991916868</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='from' value='[email protected]'/>
    <apps:property name='shouldArchive' value='true'/>
    <apps:property name='shouldTrash' value='true'/>
</entry>
<entry>
    <category term='filter'></category>
    <title>Mail Filter</title>
    <id>tag:mail.google.com,2008:filter:1284993579743</id>
    <updated>2010-10-28T11:59:31Z</updated>
    <content></content>
    <apps:property name='subject' value='Some Relevant Subject'/>
    <apps:property name='label' value='MyCoolLabel'/>
    <apps:property name='shouldArchive' value='true'/>
</entry>

Other "apps:property" verbs include:

<apps:property name='hasTheWord' value=''/>
<apps:property name='shouldAlwaysMarkAsImportant' value=''/>
<apps:property name='doesNotHaveTheWord' value=''/>
+1  A: 

Jon,

Full marks for linking this from Facebook, I'd never have seen it otherwise and I was doing EXACTLY this yesterday, for a google service!! I think using XPath is a bit of a red herring, let me show you how to access the elements and hopefully it will give you enough information for what you want to do.

You're on the right tracks with $xml->getNamespaces() - but don't iterate there, you need to use the namespace you want on the parent element you want. Firstly, you need to work only on the entry elements, since these are the majority, you might as well loop and check if you have the right one:

foreach($xml as $tag) {
  if($tag->getName() == "entry") {
    // awesome, do Cool Stuff (TM) here
  }
}

Now, you have the element in the $tag variable, and you want the apps: namespaced child elements. So do this:

$tag->getChildren($namespaces['apps']);

When you iterate over that collection, you will see the info you wanted.

HTH,

Lorna

Lorna Mitchell
I gave this a whirl, and found that it's $tag->children('apps', TRUE) to get to the data I was after, and then, as you said, iterate over that data ($children = $tag->children... as $child) and then pull the data out of that with $child->attributes(). Thanks Lorna!
JonTheNiceGuy