0

These 2 lines below currently work within the XML document to produce the 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 to <image id="1">
$image2         current value = url working url within xml document require assignment to <image   id="2">
$output .= "<url>".$image1."</url>\n";         example of current working value 

Desired outcome the above working in code snippet currently working in the 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>";`
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
cybernaut
  • 21
  • 6

1 Answers1

1

SimpleXML library have method addAttribute

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

But I recomend to use DomDocument to work with xml it is much stronger


This is a working example of what you 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();
?>
Agrest
  • 234
  • 2
  • 15
  • Agrest@ Thank you for your response But this does not work and kills the xml document output :-( and DomDoc not an option :-( – cybernaut May 14 '13 at 09:20
  • Instance of which class is $image1? What do You mean by "kills the xml document..." how document look after addAttribute? Any warnings/errors apears while perform script? – Agrest May 14 '13 at 10:26
  • Agrest@ Thanks again, $image1 is a url, I'm using Dreamweaver CS5.5 to create PHP/xml with $image1->addAttribute('id', '1'); there are no errors when inserting this code or code hints, the problem is when I publish the document and view it in the browser its a blank document with feed lines :-( Many Thanks – cybernaut May 14 '13 at 13:05
  • @cybernaut When you get a blank output, it usually means a PHP error; you need to find the PHP error log on your server, or (temporarily) output errors to the public with `ini_set('display_errors', true)`. – IMSoP May 15 '13 at 18:05
  • @Agrest Do you have any reason to think that the OP needs to use the much more complex DOM API in this case? SimpleXML is perfectly adequate for most use cases and a lot easier to use. – IMSoP May 15 '13 at 18:06
  • @IMSoP For such cases simpleXML is definitely sufficient, but if you want to do something more(often you do) it gonna make You cry – Agrest May 15 '13 at 20:53
  • @Agrest My advice would be to switch to the DOM when you really need its complexity, and no sooner - it's a much more complex API, and most code will just look a million times uglier if rewritten to use it prematurely. If necessary, you can even mix the two (since PHP can re-wrap objects with `simplexml_import_dom` and `dom_import_simplexml` with minimal cost) - a function could take a SimpleXML node, do some complex DOM manipulation, then return it back as SimpleXML. – IMSoP May 15 '13 at 22:10
  • @IMSoP IMHO Properly used DOM isn't uglier than simpleXML. 'If necessary, you can even mix the two' this is the ugly way for me. You think that DOM has more complex API maybe but after few projects You get used to it. After use of each I prefer DOM. Here You can find some about this two: http://stackoverflow.com/questions/4803063/what-the-difference-between-phps-dom-and-simplexml-extensions – Agrest May 16 '13 at 06:45
  • Yes, "ugly" is rather subjective. I guess what I meant was that DOM code will generally require more lines of code than the equivalent SimpleXML version, because SimpleXML includes a lot of short-cuts to let you write things like `$node->child->grandChild[2]['attribute']`. I remain unconvinced that new users should be discouraged from learning SimpleXML just because they *might* one day need DOM as well. – IMSoP May 16 '13 at 08:25