PHP使用SimpleXML处理命名空间 [英] PHP Handling Namespace with SimpleXML

查看:107
本文介绍了PHP使用SimpleXML处理命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}
?>

我要做的就是将内容保存在name元素内.

All I'm trying to do is get the content inside the name element.

推荐答案

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

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

$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>";
}

在这里,值"$ node-> prefix"将是包含描述"的标签的命名空间.

here., the value, "$node->prefix" will be the namespace of the tag containing "description".

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

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

这篇关于PHP使用SimpleXML处理命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆