PHP namespace simplexml problems

丶灬走出姿态 提交于 2019-11-26 03:31:52

问题


Evening guys.

Firstly to say, I have read How do I parse XML containing custom namespaces using SimpleXML?.

I\'m parsing an XML document from a source not mind, and they use a custom namespace.

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\" xmlns:moshtix=\"http://www.moshtix.com.au\">
  <channel>
   <item>
    <link>qweqwe</link>
    <moshtix:genre>asdasd</moshtix:genre>
...

For example. When I parse using SimpleXML, none of the mostix: namespace elements are on show or accessible. Probably a really simple solution, but any ideas guys?


回答1:


Usually, people use children().

$rss = simplexml_load_string(
    '<?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0" xmlns:moshtix="http://www.moshtix.com.au">
        <channel>
            <link>qweqwe</link>
            <moshtix:genre>asdasd</moshtix:genre>
        </channel>
    </rss>'
);

foreach ($rss->channel as $channel)
{
    echo 'link: ', $channel->link, "\n";
    echo 'genre: ', $channel->children('moshtix', true)->genre, "\n";
}


来源:https://stackoverflow.com/questions/2098170/php-namespace-simplexml-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!