日期:2014-05-16  浏览次数:20525 次

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

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


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

$str = <<<EOT
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
    <cas:authenticationSuccess>
        <cas:user>zhangjian</cas:user>
        <cas:language></cas:language>
    </cas:authenticationSuccess>
</cas:serviceResponse>
EOT;


/*
$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。
------解决方案--------------------
怎么会?
<xmp>
<?php
$str = <<<EOT
<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
    <cas:authenticationSuccess>
        <cas:user>zhangjian</cas:user>
        <cas:language></cas:language>
    </cas:authenticationSuccess>
</cas:serviceResponse>
EOT;
 
 

$validateXML = simplexml_load_string($str, null, 0, 'cas', true);
print_r($validateXML);

$successnode = $validateXML->authenticationSuccess[0];
print_r($successnode);

 
$xml = new DOMDocument();
$xml -> loadXML($str);
print_r($xml);
SimpleXMLElement Object
(
    [authenticationSuccess] => SimpleXMLElement Object
        (
            [user] => zhangjian
            [language] => SimpleXMLElement Object
                (
                )

        )

)
SimpleXMLElement Object
(
    [user] => zhangjian
    [language] => SimpleXMLElement Object
        (
        )

)
DOMDocument Object
(
    [doctype] => 
    [implementation] => (object value omitted)
    [documentElement] =>&