php函数simplexml_load_string转xml的小坑

发布时间:2020-04-14 04:13:25 作者:daydaydream
来源:网络 阅读:16063

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)

推荐阅读:
  1. Unity WebGL与IIS小坑
  2. php数组转xml

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 函数 simplexml

上一篇:CISCO交换机恢复出厂设置

下一篇:Ora - 20005

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》