2010-10-28 46 views
2

由于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=''/> 
+0

问题是什么?作为未成年人,以这种方式从输入源获取名称空间声明并不方便。您应该事先了解您的输入模式,并相应地将名称空间URI添加到前缀绑定中。 – 2010-10-28 14:15:34

+0

我不明白这与XPath有什么关系? – 2010-10-28 16:50:27

+0

感谢Dimitre - 我注意到有人已经删除了xpath标记。对不起,我对simplexml非常陌生,我认为这两者是交织在一起的! – JonTheNiceGuy 2010-10-29 09:01:34

回答

1

乔恩,

从Facebook链接这个满分,我从来没有看到它,否则我昨天做了正确的,这是一个谷歌服务!我认为使用XPath是一个红鲱鱼,让我告诉你如何访问元素,并希望它会给你足够的信息,你想做什么。

你在$ xml-> getNamespaces()的正确轨道上 - 但不要在那里迭代,你需要在你想要的父元素上使用你想要的命名空间。首先,你只需要在entry元素的工作,因为这是多数时,不妨循环和检查,如果你有正确的:

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

现在,你在$标记变量的元素,并且你想要apps:namespaced子元素。所以这样做:

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

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

HTH,

罗娜

+0

我给了它一个旋风,发现它是$ tag-> children('apps',TRUE)以获取我之前的数据,然后如您所说,迭代该数据($ children = $ tag- > children ... as $ child),然后使用$ child-> attributes()从数据中提取数据。谢谢Lorna! – JonTheNiceGuy 2010-10-29 09:30:36

2

我最近也有类似的 “问题”。我正尝试从Google Apps获取电子邮件签名设置。这是从谷歌XML响应的样子:

<?xml version='1.0' encoding='UTF-8'?> 
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'> 
<id>https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/signature</id> 
<updated>2012-08-31T15:22:03.067Z</updated> 
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/[email protected]'/> 
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/emailsettings/2.0/domain.com/user/[email protected]'/> 
<apps:property name='signature' value='Here goes the email signature...'/> 
</entry> 

我是不是能够得到的属性进行应用程序:使用的SimpleXMLElement财产。这是我终于如何解决它:

// create SimpleXMLElement with XML input as string 
    $xml = new SimpleXMLElement($feed); 

    // get namespace from XML 
    $namespaces = $xml->getNamespaces(true); 

    // get children (using namespace) and attributes 
    $appsProperty = $xml->children($namespaces['apps'])->attributes(); 

    // attributes are now stored in array $appsProperty and can be accessed 
    echo "Name: ".$appsProperty['name']; 
    echo "Signature: ".$appsProperty['value'];