- //xml string
- $xml_string="<?xml version='1.0'?>
- <users>
- <user id='398'>
- <name>Foo</name>
- <email>foo@bar.com</name>
- </user>
- <user id='867'>
- <name>Foobar</name>
- <email>foobar@foo.com</name>
- </user>
- </users>";
- //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, '<br />';
- }
json數據解析代碼如下:
- $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
- //xml string
- $xml_string="<?xml version='1.0'?>
- <users>
- <user id='398'>
- <name>Foo</name>
- <email>foo@bar.com</name>
- </user>
- <user id='867'>
- <name>Foobar</name>
- <email>foobar@foo.com</name>
- </user>
- </users>";
- //load the xml string using simplexml
- $xml = simplexml_load_string($xml_string);
- //loop through the each node of user
- foreach ($xml->user as $user)
- {//開源代碼Vevb.com
- //access attribute
- echo $user['id'], ' ';
- //subnodes are accessed by -> operator
- echo $user->name, ' ';
- echo $user->email, '<br />';
- }
php還自帶了一個PHP XML Parser
PHP XML Parser 簡介
XML 函數允許我們解析 XML 文檔,但無法對其進行驗證,XML 是一種用于標準結構化文檔交換的數據格式.
新聞熱點
疑難解答