PHP reads XML file data

Source: Internet
Author: User

Recently, when accessing the channel, the interface returned is XML data. Now the interface data return JSON data format is more common.

How to get real data inside XML?

Demo Example:

Create the XML file: Test.xml, the PHP file that is stored in and read under the same level directory.

XML file:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <node>3     <ID>10001</ID>4     <UserName>Admin</UserName>5     <node>6         <token>Secretkey</token>7     </node>8 </node>

PHP Files:

1<?PHP2     $file= ' Test.xml ';3     $xml _object=simplexml_load_file($file);//reads the data from the XML into the array object4     Var_dump($xml _object);5     //All object Result collection6 //object (simplexmlelement) #1 (3) {["id"]=> string (5) "10001" ["UserName"]=> string (5) "admin" ["Node"]=> Object (SimpleXMLElement) #2 (1) {["token"]=> string (9) "Secretkey"}}7     Echo' <pre> ';8 9     Var_dump($xml _object-ID);Ten     //$xml _object->id Object Values One //object (simplexmlelement) #2 (1) {[0]=>string (5) "10001"} A     Echo' <pre> '; -  -     Echo($xml _object-ID); the     //$xml _object->id Object Values - //10001 (echo cast result) -     Echo' <pre> '; -  +     //note that getting real parameter values in the code requires us to do the cast processing -     Var_dump((string)$xml _object-ID); +     //$xml _object->id Object Values A //string (5) "10001" at     Echo' <pre> '; -  -     Var_dump((string)$xml _object->node->token); -     //$xml _object->node->token Object Values - //string (9) "Secretkey"

Results Print:

PHP reads XML file data

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.