0

Hello to all i have a little problem, i have one array like this

$slike=array('1.jpg','2.jpg')

And another XML that looks like this

<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
</settings>

How to insert $slike in that XML, that new XML looks like this:

<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
<image>1.jpg</image>
<image>2.jpg</image>
</settings>

Txanks in advance

1
  • Just what is your problem? Commented Dec 24, 2012 at 21:11

3 Answers 3

1

No idea into which problem you did run, but it's rather trivial using an XML library, e.g. even with SimpleXML in your case:

$slike = array('1.jpg','2.jpg');
$name = 'image';

$doc = new SimpleXMLElement($xml);
foreach($slike as $file) {
    $doc->addChild($name, $file);
}

echo $doc->asXML();

The $xml in this example is the xml string from your question. Output:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>
<image>1.jpg</image><image>2.jpg</image></settings>
Sign up to request clarification or add additional context in comments.

4 Comments

This is close to good, thanks, can you modified code a little more, because i have config_skin_round.xml in seperate file, and i need new xml to create to parsed into javascript
You mean you want to save it to file? You can do so as well with the SimpleXMLElement::asXML(); method, just provide the filename as string. Also you can load from a file with simplexml_load_file - does this help?
i dont want echo $doc->asXML; i want t save new galerija.xml
See the first argument of: asXML($filename_here); - see the link in my previous comment, the PHP manual describes in detail how that works. I couldn't do better.
0

What about something like this:

header("Content-Type: text/xml; charset=utf-8");  // added this line - that way the browser will render it as XML
$slike = array('1.jpg','2.jpg');

$xml = "<?xml version='1.0' encoding='UTF-8'?>
<settings>
<show_context_menu>yes</show_context_menu>
<hide_buttons_delay>2</hide_buttons_delay>
<thumbs_width>200</thumbs_width>
<horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
<buttons_margins>0</buttons_margins>";

foreach($slike as $s){
    $xml.= "<image>".$s."</image>"; // create a new <image> node for each image in array
}


$xml .= "</settings>";

print_r($xml);

Outputs this:

<?xml version='1.0' encoding='UTF-8'?>
<settings>
    <show_context_menu>yes</show_context_menu>
    <hide_buttons_delay>2</hide_buttons_delay>
    <thumbs_width>200</thumbs_width>
    <horizontal_space_btween_buttons>0</horizontal_space_btween_buttons>
    <buttons_margins>0</buttons_margins>
    <image>1.jpg</image>
    <image>2.jpg</image>
</settings>

3 Comments

Interesting, you made a string from XML :) But what if the XMl is too long :))
I want to keep all in XML, not string :)
So you have an xml file that you want to add the array contents to... do you want to do this temporarily or permanently? The xml can be any length really and it will still work, will just slow down. What exactly are you trying to do - your post doesn't explain very well
0

http://php.net/manual/en/book.simplexml.php

The syntax looks something like this for your example

<aaaa Version="1.0">
   <bbb>
     <cccc>
       <dddd Id="id:pass" />
       <eeee name="hearaman" age="24" />
     </cccc>
   </bbb>
</aaaa>

 $xml = new SimpleXMLElement($xmlString);
    echo $xml->bbb->cccc->dddd['Id'];
    echo $xml->bbb->cccc->eeee['name'];
    // or...........
    foreach ($xml->bbb->cccc as $element) {
      foreach($element as $key => $val) {
       echo "{$key}: {$val}";
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.