PHP使用SimpleXML处理名称空间

PHP使用SimpleXML处理名称空间,php,xml,namespaces,xml-namespaces,Php,Xml,Namespaces,Xml Namespaces,我真的需要使用名称空间的帮助。如何使以下代码正常工作 <?php $mytv = simplexml_load_string( '<?xml version="1.0" encoding="utf-8"?> <mytv> <mytv:channelone> <mytv:description>comedy that makes you laugh</mytv:description> </m

我真的需要使用名称空间的帮助。如何使以下代码正常工作

<?php
$mytv = simplexml_load_string(
'<?xml version="1.0" encoding="utf-8"?>
 <mytv>
    <mytv:channelone>
        <mytv:description>comedy that makes you laugh</mytv:description>
    </mytv:channelone>
 </mytv>'
);

foreach ($mytv as $mytv1)
{
    echo 'description: ', $mytv1->children('mytv', true)->channelone->description;
}
?>
children('mytv',true)->channelone->description;
}
?>

我所要做的就是获取name元素中的内容。

当您使用xml中的名称空间时,您应该定义您所使用的名称空间。。!在我发布的代码中,您可以看到如何定义正在使用的命名空间

您需要显示特定于命名空间的描述,不是吗。。?如果我错了,请纠正我。请适当地张贴你的目的,以便我能理解你的问题

使用这段代码,看看你是否能得到一些想法

$xml ='<mytv>
 <mytv:channelone xmlns:mytv="http://mycompany/namespaces/mytvs">
    <mytv:description >comedy that makes you laugh</mytv:description>
 </mytv:channelone>
</mytv>';

$xml = simplexml_load_string($xml);

$doc = new DOMDocument();  
$str = $xml->asXML(); 
$doc->loadXML($str); 

$bar_count = $doc->getElementsByTagName("description");

foreach ($bar_count as $node) 
{   
echo $node->nodeName." - ".$node->nodeValue."-".$node->prefix. "<br>";
}
$xml='1!'
让你发笑的喜剧
';
$xml=simplexml\u load\u字符串($xml);
$doc=新的DOMDocument();
$str=$xml->asXML();
$doc->loadXML($str);
$bar_count=$doc->getElementsByTagName(“说明”);
foreach($bar\u计为$node)
{   
echo$node->nodeName.“-”$node->nodeValue.“-”$node->prefix.“
”; }
在此,值“$node->prefix”将是包含“description”的标记的命名空间

getElementsByTagName(“description”)用于获取xml中包含作为标记的描述的所有元素。。。!!然后稍后使用“$node->prefix”将您与所需的特定名称空间进行比较,然后打印..

Awesome:)一定要将其付诸实践,并学习不同的使用方法。