<?php
function
xml_parser(
$str
){
$xml_parser
= xml_parser_create();
if
(!xml_parse(
$xml_parser
,
$str
,true)){
xml_parser_free(
$xml_parser
);
return
false;
}
else
{
return
(json_decode(json_encode(simplexml_load_string(
$str
)),true));
}
}
$xmlstr
=<<<ETO
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>XML数据</author>
<title>PHP中文网</title>
<publisher>tools.jb51.net</publisher>
</book>
</books>
ETO;
$jsonstr
='{
"tools"
: [ {
"name"
:
"css format"
,
"site"
:
"http://tools.jb51.net/code/css"
}, {
"name"
:
"json format"
,
"site"
:
"http://tools.jb51.net/code/json"
}, {
"name"
:
"pwd check"
,
"site"
:
"http://tools.jb51.net/password/my_password_safe"
} ] }';
if
(xml_parser(
$xmlstr
)){
echo
"\$xmlstr是xml格式数据"
;
}
else
{
echo
"\$xmlstr不是xml格式数据"
;
}
echo
"<br/>"
;
if
(xml_parser(
$jsonstr
)){
echo
"\$jsonstr是xml格式数据"
;
}
else
{
echo
"\$jsonstr不是xml格式数据"
;
}
?>