Jump to content

Passing xml to php


redyard

Recommended Posts

A simple example using simplexml

 

<?php
$inventory = array();

$xml = simplexml_load_string ("<inventory>
   <widget>
      <name>One</name>
      <color>Red</color>
      <weight>5</weight>
      <price>15</price>
   </widget>
   <widget>
      <name>Two</name>
      <color>Blue</color>
      <weight>8</weight>
      <price>21</price>
   </widget>
   <widget>
      <name>Three</name>
      <color>Green</color>
      <weight>3</weight>
      <price>12</price>
   </widget>
   <widget>
      <name>Four</name>
      <color>Red</color>
      <weight>15</weight>
      <price>35</price>
   </widget>
</inventory>");

$xp = $xml->xpath('//widget');

$count = 0;
foreach ($xp as $w)
{    
    $inventory[$count] = array ();
    foreach ($w->children() as $child )
    {
       $inventory[$count][] = (string)$child;
    }
    ++$count;
}

echo '<pre>', print_r($inventory, true), '</pre>';
?>

Link to comment
https://forums.phpfreaks.com/topic/68333-passing-xml-to-php/#findComment-343608
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.