Home  >  Article  >  Backend Development  >  A small example of php parsing JSON and XML

A small example of php parsing JSON and XML

WBOY
WBOYOriginal
2016-07-25 09:04:121141browse
  1. $json_string='{"id":1,"name":"foo","email":"foo@foobar.com","interest":["wordpress","php"]} ';
  2. $obj=json_decode($json_string);
  3. echo $obj->name; //prints foo
  4. echo $obj->interest[1]; //prints php
  5. ?>
复制代码

2、解析XML 数据

I)、xml文件

  1. $xml_string="
  2. Foo
  3. foo@bar.com
  4. Foobar
  5. foobar@foo.com
  6. ";
复制代码

II)、解析xml的代码

  1. //load the xml string using simplexml

  2. $xml = simplexml_load_string($xml_string);

  3. //loop through the each node of user

  4. foreach ($xml->user as $user)
  5. {
  6. //access attribute
  7. echo $user['id'], ' ';
  8. //subnodes are accessed by -> operator
  9. echo $user->name, ' ';
  10. echo $user->email, '
    ';
  11. }
  12. ?>

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn