由于GMail使得可以导入和导出邮件过滤器,我想用PHP脚本来管理从GMail导出的XML文件,因为有一些known issues与搜索过滤器中的字符数。使用PHP来管理GMail邮件过滤XML文件
我在PHP中找到了simplexml_load_file函数,并且对执行的导出执行了var_dump(),但我现在似乎无法访问生成的XML文件中的应用程序名称空间。
遵循PHP手册中的各种页面,我创建了这个非常简单的脚本来读取XML,因此我可以开始处理已创建的过滤器。不幸的是,它似乎缺少关键部分!
<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>
这将返回该数据(摘录)
["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) {
}
}
下面是我今天下载了XML文件中提取,用于创建上面的输出:
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<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>
其他“应用:财产“动词包括:
<apps:property name='hasTheWord' value=''/>
<apps:property name='shouldAlwaysMarkAsImportant' value=''/>
<apps:property name='doesNotHaveTheWord' value=''/>
问题是什么?作为未成年人,以这种方式从输入源获取名称空间声明并不方便。您应该事先了解您的输入模式,并相应地将名称空间URI添加到前缀绑定中。 – 2010-10-28 14:15:34
我不明白这与XPath有什么关系? – 2010-10-28 16:50:27
感谢Dimitre - 我注意到有人已经删除了xpath标记。对不起,我对simplexml非常陌生,我认为这两者是交织在一起的! – JonTheNiceGuy 2010-10-29 09:01:34