0

こんにちは、少し問題があります。このような配列が 1 つあります。

$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

このようなものはどうですか:

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 に答える