成都网站建设设计

将想法与焦点和您一起共享

php函数simplexml_load_string转xml的小坑

1、如下代码报错:

成都创新互联是专业的呼中网站建设公司,呼中接单;提供成都网站建设、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行呼中网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

$xml = " < ![CDATA[toUser] ]> < ![CDATA[fromUser] ]> 1348831860 < ![CDATA[text] ]> < ![CDATA[this is a test] ]> 1234567890123456 ";
$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数据,调试通过,返回值成功

$xml = "






1348831860






1234567890123456
";
$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)


名称栏目:php函数simplexml_load_string转xml的小坑
链接地址:http://chengdu.cdxwcx.cn/article/pjpido.html