您好,登錄后才能下訂單哦!
1、如下代碼報錯:
<?php
$xml = "<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[fromUser] ]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[this is a test] ]></Content> <MsgId>1234567890123456</MsgId> </xml>";
$obj=simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
var_dump($obj);
$obj1=simplexml_load_string($xml,'SimpleXMLElement');
var_dump($obj1);
?>
php調試錯誤提示:Warning: simplexml_load_string(): Entity: line 1: parser error : StartTag: invalid element name in C:\wamp64\www1\test1.php on line 4
2、后來重新書寫了xml數據,調試通過,返回值成功
<?php
$xml = "<xml>
<ToUserName>
<![CDATA[toUser]]>
</ToUserName>
<FromUserName>
<![CDATA[fromUser]]>
</FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType>
<![CDATA[text]]>
</MsgType>
<Content>
<![CDATA[this is a test]]>
</Content>
<MsgId>1234567890123456</MsgId>
</xml>";
$obj=simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA);
var_dump($obj);
$obj1=simplexml_load_string($xml,'SimpleXMLElement');
var_dump($obj1);
?>
3、注意加入LIBXML_NOCDATA參數后的區別,結果如下:
var_dump($obj)如下:
C:\wamp64\www1\test1.php:20:
object(SimpleXMLElement)[1]
public 'ToUserName' => string '
toUser
' (length=12)
public 'FromUserName' => string '
fromUser
' (length=14)
public 'CreateTime' => string '1348831860' (length=10)
public 'MsgType' => string '
text
' (length=10)
public 'Content' => string '
this is a test
' (length=20)
public 'MsgId' => string '1234567890123456' (length=16)
var_dump($obj1)如下:
C:\wamp64\www1\test1.php:22:
object(SimpleXMLElement)[2]
public 'ToUserName' =>
object(SimpleXMLElement)[3]
public 'FromUserName' =>
object(SimpleXMLElement)[4]
public 'CreateTime' => string '1348831860' (length=10)
public 'MsgType' =>
object(SimpleXMLElement)[5]
public 'Content' =>
object(SimpleXMLElement)[6]
public 'MsgId' => string '1234567890123456' (length=16)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。