使用PHP来管理GMail邮件过滤XML文件 [英] Using PHP to manage GMail Mail Filter XML Files

查看:128
本文介绍了使用PHP来管理GMail邮件过滤XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为GMail可以导入和导出邮件过滤器,所以我想用PHP脚本来管理从GMail导出的XML文件,因为有一些已知问题与搜索过滤器中的字符数。



我在PHP中找到了simplexml_load_file函数,并且对执行的导出执行了var_dump(),但我现在似乎无法访问生成的XML文件中的应用程序名称空间。

遵循PHP手册中的各种页面,我创建了这个非常简单的脚本来读取XML,以便我可以开始处理已创建的过滤器。不幸的是,它似乎缺少关键部分!

 < pre><?php 
$ b $ (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(无法打开过滤文件);
}

?>< / pre>

这将返回此数据(摘录)

  [ 条目] => 
array(268){
[0] =>
object(SimpleXMLElement)#3(5){
[category] =>
object(SimpleXMLElement)#271(1){
[@attributes] =>
array(1){
[term] =>
string(6)filter
}
}
[title] =>
string(11)邮件过滤器
[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)邮件过滤器
[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/应用/ 2006' > 
< title>邮件过滤器< / title>
< id>标记:mail.google.com,2008:过滤器:1284991916868,...,1287734777820< / id>
< updated> 2010-10-28T11:59:31Z< / updated>
< author>
<名称>我的名字< /名称>
< email> my@email.addr.es< / email>
< / author>
< entry>
< category term ='filter'>< / category>
< title>邮件过滤器< /标题>
< id>标记:mail.google.com,2008:过滤器:1284991916868< / id>
< updated> 2010-10-28T11:59:31Z< / updated>
< content>< / content>
< apps:property name ='from'value = an@email.addr.es'/>
< apps:property name ='shouldArchive'value ='true'/>
< apps:property name ='shouldTrash'value ='true'/>
< / entry>
< entry>
< category term ='filter'>< / category>
< title>邮件过滤器< /标题>
< id>标记:mail.google.com,2008:过滤器: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动词包括:

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


解决方案

Jon,

从Facebook上链接这个标志的完整标记,我从来没有见过它,而且我昨天正确地做了这个,对于谷歌服务!我认为使用XPath是一种红鲱鱼,让我告诉你如何访问这些元素,并希望它能为你提供足够的信息。



你正在用$ xml-> getNamespaces()正确的轨道 - 但是不要在那里迭代,你需要在你想要的父元素上使用你想要的命名空间。首先,你只需要在入口元素上工作,因为这些都是多数,你可能会循环并检查你是否有正确的:

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


$ / code $
$现在,你在$标签中有元素变量,并且您需要apps:namespaced子元素。所以这样做:

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

当您迭代该集合时,您会看到您想要的信息。



HTH,



洛娜


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'>
    <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>my@email.addr.es</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='an@email.addr.es'/>
    <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=''/>

解决方案

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

这篇关于使用PHP来管理GMail邮件过滤XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆