PHP implementation parsing XML for an array of case studies

Source: Internet
Author: User
Tags cdata
This time to bring you to the PHP implementation of parsing XML for an array of case details, the PHP implementation of parsing XML for the attention of the array of considerations, the following is the actual case, together to see.

Recently want to do a plug-in mechanism, need to use XML, when parsing XML need to convert to an array, deliberately record one such parsing method

Xmldemo.xml file:

<?xml version= "1.0" encoding= "UTF-8"? ><main xmlns= "http://www.xiaoetongo.cn" versioncode= "1.0" >< Controller co= "Aritles" ><meth title= "test plug-in" do= "Aritle"/></controller><controller co= "Ari" > <meth title= "Test plug-in" do= "ar"/><meth title= "test plug-in" do= "a"/></controller><install><! [cdata[]]></install><upgrade><! [cdata[]]></upgrade></main>

PHP Code:

<?php$xmls=file_get_contents ("Xmldemo.xml"); $xml =simplexml_load_string ($xmls); $xmljson = Json_encode ($xml); $ Xml=json_decode ($xmljson, True); Var_dump ($xml);

Operation Result:

Array (4) {["@attributes"]=> Array (1) {  ["Versioncode"]=>  string (3) "1.0"} ["Controller"]=> Array (2) {  [0]=>  Array (2) {   ["@attributes"]=>   Array (1) {    ["Co"]=>    string (7) "Aritles"   }   ["Meth"]=>   Array (1) {    ["@attributes"]=>    Array (2) {     ["title"]=>     string (12) "Test plug-in "     [" Do "]=>     string (6)" Aritle "}}  }  [1]=>  Array (2) {   [" @attributes "]=>   Array (1) {    [" Co "]=>    string (3)" Ari "   }   [" Meth "]=>   Array (2) {    [0]= >    Array (1) {     ["@attributes"]=>     Array (2) {      ["title"]=>      string (12) "Test plug-in"      [" Do "]=>      string (2)" Ar "     }    }    [1]=>    Array (1) {     [" @attributes "]=>     Array (2 {      ["title"]=>      string (12) "Test plug-in"      ["Do"]=>      string (1) "A"  }}}} [" Install "]=> Array (0) {} [" Upgrade "]=> Array (0) {}}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP calculates an integer set of extra-large numbers

PHP uses Curl to clone IP and refer steps

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.