php 如何解析xml格式字符串
展开全部
//假设xml字符串
$postStr = '<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[p1]]></Content></xml>';
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
var_dump($postObj);
/**
object(SimpleXMLElement)[16] public 'ToUserName' => string 'toUser' (length=6) public 'FromUserName' => string 'FromUser' (length=8) public 'CreateTime' => string '123456789' (length=9) public 'MsgType' => string 'text' (length=4) public 'Content' => string 'p1' (length=2)
*/
//把xml字符串解析为对象
$postStr = '<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[p1]]></Content></xml>';
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
var_dump($postObj);
/**
object(SimpleXMLElement)[16] public 'ToUserName' => string 'toUser' (length=6) public 'FromUserName' => string 'FromUser' (length=8) public 'CreateTime' => string '123456789' (length=9) public 'MsgType' => string 'text' (length=4) public 'Content' => string 'p1' (length=2)
*/
//把xml字符串解析为对象
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-10
展开全部
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询