PHP 使用 SimpleXML 处理命名空间
我真的需要使用命名空间的帮助。如何让下面的代码正常工作?
<?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;
}
?>
我想做的就是获取名称元素内的内容。
I really need help with using namespaces. How do I get the following code to work properly?
<?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;
}
?>
All I'm trying to do is get the content inside the name element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你在 xml 中使用命名空间时,你应该定义你使用的命名空间......!在我发布的代码中,您可以看到如何定义您正在使用的命名空间..
您需要显示特定于命名空间的描述,不是吗..?如果我错了,请纠正我。,请正确发布你的目的,以便我能够理解你的问题。
使用此代码,看看你是否能得到一些想法。
这里,值,“$node->前缀" 将是包含“description”的标签的命名空间。
getElementsByTagName("description") 用于获取 xml 中包含描述作为标签的所有元素...!!然后稍后使用“$node->前缀”与您需要的特定命名空间进行比较,然后打印..
when ever yu are using the namespaces in xml yu should define the namespaces what ever you use..! in the code what i posted you can see how you can define the namespace you are using..
you need to display the description specific to the namespace isn't it..? correct me if I'm wrong., and please post yur purpose properly so that i can understand your problem..
Use this code and see if you can get some idea..
here., the value, "$node->prefix" will be the namespace of the tag containing "description".
getElementsByTagName("description") is used to get all the elements in the xml containing description as tags...!! and then later using the "$node->prefix" you compare with the specific namespace as required for you and then print..