How to add "id" attributes to exsisting element values in a PHP simplexml document -


these 2 lines below work within xml document produce 2 values :

    if (isset($images[0]->fname)) { $image1 = $theimgpath.'/'.$images[0]->fname; } else  {   $image1 = ''; }     if (isset($images[1]->fname)) { $image2 = $theimgpath.'/'.$images[1]->fname; } else { $image2 = ''; }   $image1     current value working = url within xml document require assignment <image id="1"> $image2         current value = url working url within xml document require assignment <image   id="2"> $output .= "<url>".$image1."</url>\n";         example of current working value  

desired outcome above working in code snippet working in same xmldocument below:

`$output .= "<property>\n";  $id = $xml["id"];        $id = $xml->image['id'];  $output .= $string = <<<xml  <images> <image id="1"><url>id”1”</url></image> <image id="2"><url>id”2”</url></image>  </images>  xml;     $output .= "</property>"; } $output .= "</root>";` 

simplexml library have method addattribute

   $image1->addattribute('id', '1');    $image2->addattribute('id', '2'); 

but recomend use domdocument work xml stronger


this working example of need

<?php     $xml = <<<xml <images>     <image><url>id"1"</url></image>     <image><url>id"2"</url></image> </images> xml;     $images = simplexml_load_string($xml);     $images->image[0]->addattribute('id',1);     $images->image[1]->addattribute('id',2);     echo $images->asxml(); ?> 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -