A small example of PHP parsing JSON and XML

Source: Internet
Author: User
    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. ?>
Copy Code

2. Parsing XML data

I), XML file

    1. $xml _string= "
    2. Foo
    3. Foo@bar.com
    4. Foobar
    5. Foobar@foo.com
    6. ";
Copy Code

II), parsing the XML code

    1. Load the XML string using SimpleXML

    2. $xml = simplexml_load_string ($xml _string);

    3. Loop through the each node of the user

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

Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.