Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP命名空间simplexml问题_Php_Xml_Namespaces_Simplexml_Xml Namespaces - Fatal编程技术网

PHP命名空间simplexml问题

PHP命名空间simplexml问题,php,xml,namespaces,simplexml,xml-namespaces,Php,Xml,Namespaces,Simplexml,Xml Namespaces,晚上好,伙计们 首先要说的是,我读过 我从一个源解析一个XML文档,它们使用一个自定义名称空间 <?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:moshtix="http://www.moshtix.com.au"> <channel> <item> <link>qweqwe</link> <moshtix:gen

晚上好,伙计们

首先要说的是,我读过

我从一个源解析一个XML文档,它们使用一个自定义名称空间

<?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>
...

qweqwe
asdasd
...
比如说。当我使用SimpleXML解析时,mostix:namespace元素中没有一个显示或可访问。这可能是一个非常简单的解决方案,但有什么想法吗?

通常,人们会使用

$rss=simplexml\u加载\u字符串(
'
qweqwe
asdasd
'
);
foreach($rss->channel as$channel)
{
回显“链接:”,$channel->link,“\n”;
回显“流派:”,$channel->children('moshtix',true)->流派,“\n”;
}

虽然这对提取每个元素都有用,但很多时候我需要执行json_编码,并简单地将每个有效项捆绑起来并存储在数据库中。但当我这样做时,它无法识别自定义名称空间项。有什么想法吗?每个元素中的数据变化太大,无法手动输入每个元素。通过对每个元素进行几次foreach扫描,成功地解决了此问题;)当人们说他们正在将XML转换为JSON而不关心结构时,我总是有点吃惊。为什么不将其存储为XML呢?
$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";
}