首頁  >  文章  >  後端開發  >  php解析cas返回的xml解决思路

php解析cas返回的xml解决思路

WBOY
WBOY原創
2016-06-13 11:49:581467瀏覽

php解析cas返回的xml
    使用php进行cas验证时,对方返回这么一段xml

<br /><cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'><br />    <cas:authenticationSuccess><br />        <cas:user>zhangjian</cas:user><br />        <cas:language></cas:language><br />    </cas:authenticationSuccess><br /></cas:serviceResponse><br />


    于是使用php相关函数进行解析

$str = <<

/*
$validateXML = simplexml_load_string($str);print_r($validateXML);
$successnode = $validateXML->authenticationSuccess[0];
var_dump($successnode);
*/

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


    结果都解析不出来,哪位朋友遇到过请指点下,不甚感激!!!
------解决方案--------------------
SimpleXML也是PHP5后提供的一套简单易用的xml工具集,
不过它不适用于包含namespace(命名空间)的xml。
------解决方案--------------------
怎么会?
<br /><?php<br />$str = <<<EOT&lt;br /&gt;&lt;cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'&gt;&lt;br /&gt;    &lt;cas:authenticationSuccess&gt;&lt;br /&gt;        &lt;cas:user&gt;zhangjian&lt;/cas:user&gt;&lt;br /&gt;        &lt;cas:language&gt;&lt;/cas:language&gt;&lt;br /&gt;    &lt;/cas:authenticationSuccess&gt;&lt;br /&gt;&lt;/cas:serviceResponse&gt;&lt;br /&gt;EOT;<br /> <br /> <br /><br />$validateXML = simplexml_load_string($str, null, 0, 'cas', true);<br />print_r($validateXML);<br /><br />$successnode = $validateXML->authenticationSuccess[0];<br />print_r($successnode);<br /><br /> <br />$xml = new DOMDocument();<br />$xml -> loadXML($str);<br />print_r($xml);</pre></p>
<pre class="brush: text">SimpleXMLElement Object&lt;br&gt;(&lt;br&gt;    [authenticationSuccess] =&gt; SimpleXMLElement Object&lt;br&gt;        (&lt;br&gt;            [user] =&gt; zhangjian&lt;br&gt;            [language] =&gt; SimpleXMLElement Object&lt;br&gt;                (&lt;br&gt;                )&lt;br&gt;&lt;br&gt;        )&lt;br&gt;&lt;br&gt;)&lt;br&gt;SimpleXMLElement Object&lt;br&gt;(&lt;br&gt;    [user] =&gt; zhangjian&lt;br&gt;    [language] =&gt; SimpleXMLElement Object&lt;br&gt;        (&lt;br&gt;        )&lt;br&gt;&lt;br&gt;)&lt;br&gt;DOMDocument Object&lt;br&gt;(&lt;br&gt;    [doctype] =&gt; &lt;br&gt;    [implementation] =&gt; (object value omitted)&lt;br&gt;    [documentElement] =&gt; (object value omitted)&lt;br&gt;    [actualEncoding] =&gt; &lt;br&gt;    [encoding] =&gt; &lt;br&gt;    [xmlEncoding] =&gt; &lt;br&gt;    [standalone] =&gt; 1&lt;br&gt;    [xmlStandalone] =&gt; 1&lt;br&gt;    [version] =&gt; 1.0&lt;br&gt;    [xmlVersion] =&gt; 1.0&lt;br&gt;    [strictErrorChecking] =&gt; 1&lt;br&gt;    [documentURI] =&gt; file:///D:/AMP/web/&lt;br&gt;    [config] =&gt; &lt;br&gt;    [formatOutput] =&gt; &lt;br&gt;    [validateOnParse] =&gt; &lt;br&gt;    [resolveExternals] =&gt; &lt;br&gt;    [preserveWhiteSpace] =&gt; 1&lt;br&gt;    [recover] =&gt; &lt;br&gt;    [substituteEntities] =&gt; &lt;br&gt;    [nodeName] =&gt; #document&lt;br&gt;    [nodeValue] =&gt; &lt;br&gt;    [nodeType] =&gt; 9&lt;br&gt;    [parentNode] =&gt; &lt;br&gt;    [childNodes] =&gt; (object value omitted)&lt;br&gt;    [firstChild] =&gt; (object value omitted)&lt;br&gt;    [lastChild] =&gt; (object value omitted)&lt;br&gt;    [previousSibling] =&gt; &lt;br&gt;    [attributes] =&gt; &lt;br&gt;    [ownerDocument] =&gt; &lt;br&gt;    [namespaceURI] =&gt; &lt;br&gt;    [prefix] =&gt; &lt;br&gt;    [localName] =&gt; &lt;br&gt;    [baseURI] =&gt; file:///D:/AMP/web/&lt;br&gt;    [textContent] =&gt; &lt;br&gt;    &lt;br&gt;        zhangjian&lt;div class=&quot;clear&quot;&gt;
                 
              
              
        
            &lt;/div&gt;</pre></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>陳述:</span><div>本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn</div></div></div><div class="nphpSytBox"><span>上一篇:<a class="dBlack" title="PHP 显示有关问题" href="http://m.php.cn/zh-tw/faq/199104.html">PHP 显示有关问题</a></span><span>下一篇:<a class="dBlack" title="在调试一个上传文件并保存在数据库里的小程序,始终出现有关问题,向这里的大神请问" href="http://m.php.cn/zh-tw/faq/199107.html">在调试一个上传文件并保存在数据库里的小程序,始终出现有关问题,向这里的大神请问</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>相關文章</h2><em><a href="http://m.php.cn/zh-tw/article.html" class="bBlack"><i>看更多</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/zh-tw/faq/1.html" title="PHP中使用cURL實作Get和Post請求的方法" class="aBlack">PHP中使用cURL實作Get和Post請求的方法</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/zh-tw/faq/1.html" title="PHP中使用cURL實作Get和Post請求的方法" class="aBlack">PHP中使用cURL實作Get和Post請求的方法</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/zh-tw/faq/1.html" title="PHP中使用cURL實作Get和Post請求的方法" class="aBlack">PHP中使用cURL實作Get和Post請求的方法</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/zh-tw/faq/1.html" title="PHP中使用cURL實作Get和Post請求的方法" class="aBlack">PHP中使用cURL實作Get和Post請求的方法</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/zh-tw/faq/2.html" title="正規表達式中所有的表達符號(總結)" class="aBlack">正規表達式中所有的表達符號(總結)</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/zh-tw/"><b class="icon1"></b><p>首頁</p></a></li><li><a href="http://m.php.cn/zh-tw/course.html"><b class="icon2"></b><p>課程</p></a></li><li><a href="http://m.php.cn/zh-tw/wenda.html"><b class="icon4"></b><p>問答</p></a></li><li><a href="http://m.php.cn/zh-tw/login"><b class="icon5"></b><p>我的</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/zh-tw/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/zh-tw/"><b class="icon1"></b><span>首頁</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/course.html"><b class="icon2"></b><span>課程</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/article.html"><b class="icon3"></b><span>文章</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/wenda.html"><b class="icon4"></b><span>問答</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/dic.html"><b class="icon6"></b><span>詞典</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/course/type/99.html"><b class="icon7"></b><span>手册</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/xiazai/"><b class="icon8"></b><span>下載</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/zh-tw/faq/zt" title="專題"><b class="icon12"></b><span>專題</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/zh-tw/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/" >首頁</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/article.html" class="hover">文章</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/wenda.html" >問答</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/course.html" >課程</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/faq/zt" >專題</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/xiazai"  >下載</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/game" >遊戲</a></div><div class="swiper-slide"><a href="http://m.php.cn/zh-tw/dic.html" >詞典</a></div><div class="clear"></div></div></div><div class="langadivs"  ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a  onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan "  href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a  onclick="javascript:setlang('en');" class="language course-right-orders chooselan "  href="javascript:;"><span>English</span><span>(EN)</span></a><a  onclick="javascript:;" class="language course-right-orders chooselan chooselanguage"  href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a  onclick="javascript:setlang('ja');" class="language course-right-orders chooselan "  href="javascript:;"><span>日本語</span><span>(JA)</span></a><a  onclick="javascript:setlang('ko');" class="language course-right-orders chooselan "  href="javascript:;"><span>한국어</span><span>(KO)</span></a><a  onclick="javascript:setlang('ms');" class="language course-right-orders chooselan "  href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a  onclick="javascript:setlang('fr');" class="language course-right-orders chooselan "  href="javascript:;"><span>Français</span><span>(FR)</span></a><a  onclick="javascript:setlang('de');" class="language course-right-orders chooselan "  href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script>            var swiper = new Swiper('.nphpNavSwiper1', {
                slidesPerView : 'auto',
                observer: true,//修改swiper自己或子元素时,自动初始化swiper
                observeParents: true,//修改swiper的父元素时,自动初始化swiper

            });
        </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) {
    var _times = times || -1, //100次
    _interval = interval || 20, //20毫秒每次
    _self = this,
    _selector = this.selector, //选择器
    _iIntervalID; //定时器id
    if( this.length ){ //如果已经获取到了,就直接执行函数
        func && func.call(this);
    } else {
        _iIntervalID = setInterval(function() {
            if(!_times) { //是0就退出
                clearInterval(_iIntervalID);
            }
            _times <= 0 || _times--; //如果是正数就 --

            _self = $(_selector); //再次选择
            if( _self.length ) { //判断是否取到
                func && func.call(_self);
                clearInterval(_iIntervalID);
            }
        }, _interval);
    }
    return this;
}
$("table.syntaxhighlighter").wait(function() {
    $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>");
});
$(document).on("click", ".cnblogs_code_footer",function(){
      $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide();
});
$('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}});
</script></body></html>