0

大家好,我有一个小问题,我有一个这样的数组

$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>
</settings>

如何在该 XML 中插入 $slike,新的 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>
<image>1.jpg</image>
<image>2.jpg</image>
</settings>

提前 Txanks

4

3 回答 3

1

不知道您确实遇到了哪个问题,但使用 XML 库非常简单,例如,即使在您的情况下使用 SimpleXML:

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

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

echo $doc->asXML();

$xml示例中的 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>
<image>1.jpg</image><image>2.jpg</image></settings>
于 2012-12-24T22:24:11.977 回答
0

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

对于您的示例,语法看起来像这样

<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}";
      }
    }
于 2013-09-25T11:29:34.520 回答
0

像这样的东西怎么样:

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);

输出这个:

<?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>
于 2012-12-24T21:16:21.493 回答