lets say i have a very basic XML file

<products>
<item>
<name>Test 1</name>
</item>
<item>
<name>Test 2</name>
</item>

</products>

what i would like todo:

1) parse the products tree and output a drop down on each <item>
--I can do that myself easily--

2) when an item is chosen you can click edit and it will bring up each of the values within the item into a form that u can edit
-- knowing the structure im pretty sure i can handle this too--

3) when you are done editting you submit and it updates the XML <item>
--this i will definitely need help on this or a good example to get a start with--

4) at the same spot where you get a drop down of items I'd like to offer the ability to add a item to the XML... i've seen the addChildren and addAttribute but the help file is rather vague to the specifics... again, a good example here would be great

any direction, links, code snippets would be greatly appreciated... thanks alot

    ok... first off the product dump

    <?php
    
    $xml_file = file_get_contents("products.xml");
    
    $xml = simplexml_load_string($xml_file);
    
    $i = 0;
    echo '<form name="products" action="">';
    echo '<select>';
    foreach ($xml->item as $product)
    {
    echo "<option>$i: ".$product->name."</option>";
    $i++;
    }
    echo '</select>';
    echo '</form>';
    
    ?>
    

    i would like to sort the list alphabetically but i cannot seem to figure that out... i tried to assign in another foreach the names into another array but anytime i tried assigning

    $products[] = $product->name;
    

    all it did was assign the simplexml object to the array and not just the name

    also, does the xml node numbering scheme start at 0 or 1?

      For the select options, I would probably just create an array of values and then sort it:

      $options = array();
      foreach($xml->item as $product)
      {
         $options[] = $product;
      }
      // now use sort() or other array function(s) as desired
      

        i did try that, but as stated when i loop out that array it contains a bunch of simplexml objects and not just the name

          foreach ($xml->item as $product)
          {
          $products[] = $product->name;
          }
          print_r($products);
          

          gives:

          Array
          (
              [0] => SimpleXMLElement Object
                  (
                      [0] => Name 1
                  )
          
          [1] => SimpleXMLElement Object
              (
                  [0] =>  Name 2
              )
          
          [2] => SimpleXMLElement Object
              (
                  [0] =>  Name 3
              )
          
          [3] => SimpleXMLElement Object
              (
                  [0] =>  Name 4
              )
          
          [4] => SimpleXMLElement Object
              (
                  [0] =>  Name 5
              )
          )
          

          but if i go:

          foreach ($xml->item as $product)
          {
          echo $product->name;
          }
          

          i get a list of just the names... WTH is that? lol

            hrm... i had to request a string object

            $products[] = (string) $product->name;
            

              ok... im all set except that i have no idea how to edit/create nodes in the existing xml structure

              im not having much success finding examples online either... if anyone can help guide me Id really appreciate it!

                Assume $xml is your SimpleXMLElement object:

                // add a new element at the top level:
                $child = $xml->addChild('element_name', 'element value goes here');
                // add an attribute to that new child element:
                $child->addAttribute('attribute_name', 'attribute value');
                
                // add a "grandchild" to that new element:
                $grandchild = $child->addElement('element_name', 'element value');
                
                  5 days later

                  well i got that... but i cant seem to actually write that out to the XML file... just the screen

                    To write to an XML file:

                    $xml->asXML('path/to/file.xml');
                    
                      Write a Reply...