Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert a given XML into PHP array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 96
    Comment on it

    Hello Reader's!, If you are developing the website that takes input in XML and you want it to convert in PHP, Then you have to use some of the library extensions to do so.

    Let suppose you have the XML as given below:-

    <tag1Version="1.0">
       <tag2>
         <main>
           <body Id="id:pass" />
           <content name="hearaman" age="24" />
         </body>
       </tag2>
    </tag1>
    

    The syntax looks something like this for your example

    $xml = new SimpleXMLElement($xmlString); echo $xml->tag1->tag2->main['Id']; echo $xml->tag2->body ->content ['name'];

    // or...........

    foreach ($xml->tag2->body as $element) {
      foreach($element as $key => $val) {
       echo "{$key}: {$val}";
      }
    }
    

    You can also have to details of this extension from here

    And another way to achieve it is as below:-

     $xml = simplexm_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
        $json = json_;encode($xml);
        $array = json_decode($json,TRUE);
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: