php解析JSON与XML的小例子
时间:2021-07-01 10:21:17
帮助过:38人阅读
- $json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} ';
- $obj=json_decode($json_string);
- echo $obj->name; //prints foo
- echo $obj->interest[1]; //prints php
- ?>
2、解析XML 数据
I)、xml文件
- $xml_string="
- Foo
- foo@bar.com
- Foobar
- foobar@foo.com
";
II)、解析xml的代码
//load the xml string using simplexml - $xml = simplexml_load_string($xml_string);
//loop through the each node of user - foreach ($xml->user as $user)
- {
- //access attribute
- echo $user['id'], ' ';
- //subnodes are accessed by -> operator
- echo $user->name, ' ';
- echo $user->email, '
'; - }
- ?>
|