PHP:如何使用嵌套的xpath元素解析XML

时间:2023-01-15 00:12:13

Here is the XML that I am working on :

这是我正在处理的XML:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
    <channel>
        <item>
            <title>A Simple Title</title>
            <noo:subcategory>the sub category</noo:subcategory>
            <noo:relatedInfos>
                <noo:teams>
                    <noo:team id="3">New York</noo:team>
                    <noo:team id="4">Las Vegas</noo:team>
                </noo:teams>
            </noo:relatedInfos>
        </item>
    </channel>
</rss>

I am doing this php code to get the two "team" but it does not work ($xml has the previous content) :

我正在做这个PHP代码来获得两个“团队”,但它不起作用($ xml具有以前的内容):

$xml_datas = simplexml_load_string($xml);

foreach($xml_datas->channel->item as $item){                      
    $noo = $item->children('noo');
    echo $noo->team;
}

Do you have any idea why it is not working ?

你知道它为什么不起作用吗?

Thanks

2 个解决方案

#1


0  

See if this helps:

看看这是否有帮助:

<?php // RAY_temp_userco.php
error_reporting(E_ALL);

$xml = <<<ENDXML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
    <channel>
        <item>
            <title>A Simple Title</title>
            <noo:subcategory>the sub category</noo:subcategory>
            <noo:relatedInfos>
                <noo:teams>
                    <noo:team id="3">New York</noo:team>
                    <noo:team id="4">Las Vegas</noo:team>
                </noo:teams>
            </noo:relatedInfos>
        </item>
    </channel>
</rss>
ENDXML;

$obj = simplexml_load_string($xml);

$ns = $obj->getNamespaces(TRUE);

foreach($obj->channel->item as $item){
    $noo = $item->children($ns['noo']);
    var_dump($noo);
}

#2


0  

"noo" is just a local alias for that namespace, and the ->children() method (and most XML handling functions) want to know its actual global identifier, which is the URI in the xmlns attribute.

“noo”只是该命名空间的本地别名,而 - > children()方法(以及大多数XML处理函数)想要知道它的实际全局标识符,即xmlns属性中的URI。

You need to either specify the full identifier of the namespace (i.e. ->children('http://www.myscheme.com/schema')) or set the optional second parameter to tell SimpleXML to look up the prefix (->children('noo', true). The second may be more readable, but it will break if a future document has the same schema, but gives the namespace a different local alias.

您需要指定命名空间的完整标识符(即 - > children('http://www.myscheme.com/schema'))或设置可选的第二个参数以告诉SimpleXML查找前缀( - > children ('noo',true)。第二个可能更具可读性,但如果未来的文档具有相同的模式,它将会中断,但会为命名空间提供不同的本地别名。

Additionally, the team nodes aren't directly under the item node, so you need to traverse further to get them:

此外,团队节点不直接位于项目节点下,因此您需要进一步遍历以获取它们:

// Give the namespace a readable name that won't change
define('NS_NOO', 'http://www.myscheme.com/schema');

$xml_datas = simplexml_load_string($xml);

foreach($xml_datas->channel->item as $item){                      
    $teams = $item->children(NS_NOO)->relatedInfo->teams;
    echo $teams->team[0];
}

#1


0  

See if this helps:

看看这是否有帮助:

<?php // RAY_temp_userco.php
error_reporting(E_ALL);

$xml = <<<ENDXML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:noo="http://www.myscheme.com/schema">
    <channel>
        <item>
            <title>A Simple Title</title>
            <noo:subcategory>the sub category</noo:subcategory>
            <noo:relatedInfos>
                <noo:teams>
                    <noo:team id="3">New York</noo:team>
                    <noo:team id="4">Las Vegas</noo:team>
                </noo:teams>
            </noo:relatedInfos>
        </item>
    </channel>
</rss>
ENDXML;

$obj = simplexml_load_string($xml);

$ns = $obj->getNamespaces(TRUE);

foreach($obj->channel->item as $item){
    $noo = $item->children($ns['noo']);
    var_dump($noo);
}

#2


0  

"noo" is just a local alias for that namespace, and the ->children() method (and most XML handling functions) want to know its actual global identifier, which is the URI in the xmlns attribute.

“noo”只是该命名空间的本地别名,而 - > children()方法(以及大多数XML处理函数)想要知道它的实际全局标识符,即xmlns属性中的URI。

You need to either specify the full identifier of the namespace (i.e. ->children('http://www.myscheme.com/schema')) or set the optional second parameter to tell SimpleXML to look up the prefix (->children('noo', true). The second may be more readable, but it will break if a future document has the same schema, but gives the namespace a different local alias.

您需要指定命名空间的完整标识符(即 - > children('http://www.myscheme.com/schema'))或设置可选的第二个参数以告诉SimpleXML查找前缀( - > children ('noo',true)。第二个可能更具可读性,但如果未来的文档具有相同的模式,它将会中断,但会为命名空间提供不同的本地别名。

Additionally, the team nodes aren't directly under the item node, so you need to traverse further to get them:

此外,团队节点不直接位于项目节点下,因此您需要进一步遍历以获取它们:

// Give the namespace a readable name that won't change
define('NS_NOO', 'http://www.myscheme.com/schema');

$xml_datas = simplexml_load_string($xml);

foreach($xml_datas->channel->item as $item){                      
    $teams = $item->children(NS_NOO)->relatedInfo->teams;
    echo $teams->team[0];
}