获取遍历xml simplexml_load_file的Url匹配

时间:2015-08-06 06:14:50

标签: php xml xml-parsing

这是XML文件

    <theme name="front">
        <layouts url="/administrator" tpl="default.html" >
            <templates>
                <login url="/login" tpl="login.html"></login>
                <dashboard url="/" tpl="dashboard.html"></dashboard>
                <contact url="/contacts" tpl="contact.html" >
                    <templates>
                        <add url="/add"></add>
                        <edit url="/modify" ></edit>
                        <remove url="/remove" ></remove>
                        <bulk_view url="/bulk-view"></bulk_view>
                    </templates>
                </contact>
                <help url="/help" tpl="content.html"></help>
                <settings url="/settings" tpl="content.html"></settings>
            </templates>
        </layouts>
    </theme>

我有两个问题:

  1. 如何制作如下所示的树形网址数组。

    /administrator
    /administrator/
    /administrator/login
    /administrator/contacts
    /administrator/contacts/add
    /administrator/contacts/modify
    /administrator/contacts/bulk-view
    /administrator/help
    /administrator/settings
    
  2. 我有一个匹配/administrator/contacts/add的字符串网址。如何获得与此字符串URL匹配的SimpleXMLElement对象?

1 个答案:

答案 0 :(得分:0)

最好通过查询文档来查找您要查找的信息。对于XML,使用Xpath最好。

例如,如果要获取具有 url 属性的所有元素,则查询为:

//*[@url]

如果您现在考虑在文档中任何这样的元素,您可以在祖先上以文档顺序检索所有 url 属性 - 或者自我 xpath-axis

ancestor-or-self::*/@url

这只是针对裸xpath查询到目前为止。当使用PHP的XML库(此处为示例 SimpleXML )包装在PHP代码中时,它就会生效。

<?php
/**
 * Get Url match traversing xml simplexml_load_file (Example #1)
 *
 * @link http://stackoverflow.com/q/31848077/367456
 */
$string = <<<XML
<theme name="front">
    <layouts url="/administrator" tpl="default.html">
        <templates>
            <login url="/login" tpl="login.html"></login>
            <dashboard url="/" tpl="dashboard.html"></dashboard>
            <contact url="/contacts" tpl="contact.html">
                <templates>
                    <add url="/add"></add>
                    <edit url="/modify"></edit>
                    <remove url="/remove"></remove>
                    <bulk_view url="/bulk-view"></bulk_view>
                </templates>
            </contact>
            <help url="/help" tpl="content.html"></help>
            <settings url="/settings" tpl="content.html"></settings>
        </templates>
    </layouts>
</theme>
XML;

$xml = simplexml_load_string($string);

foreach ($xml->xpath('//*[@url]') as $urlElement) {
    echo implode("", $urlElement->xpath('ancestor-or-self::*/@url')), "\n";
}

结果是(同样:online demo):

/administrator
/administrator/login
/administrator/
/administrator/contacts
/administrator/contacts/add
/administrator/contacts/modify
/administrator/contacts/remove
/administrator/contacts/bulk-view
/administrator/help
/administrator/settings

要匹配这些网址中的任何一个,您需要查看它是否是您要查找的网址:

$search = '/administrator/contacts/add';

foreach ($xml->xpath('//*[@url]') as $urlElement) {
    $url = implode("", $urlElement->xpath('ancestor-or-self::*/@url'));
    if ($url !== $search) {
        continue;
    }
    // found!
    $urlElement->asXML('php://output');
    break;
}

哪个输出匹配的第一个元素:

<add url="/add"/>