【问题标题】:How insert PHP array in XML?如何在 XML 中插入 PHP 数组?
【发布时间】:2012-12-24 21:08:07
【问题描述】:

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

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

提前付款

【问题讨论】:

  • 你的问题是什么?

标签: php xml arrays foreach


【解决方案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>

【讨论】:

  • 这已经接近了,谢谢,你能再修改一下代码吗,因为我在单独的文件中有 config_skin_round.xml,我需要创建新的 xml 来解析成 javascript
  • 您的意思是要将其保存到文件中?您也可以使用SimpleXMLElement::asXML(); method 执行此操作,只需提供文件名作为字符串。您也可以使用 simplexml_load_file 从文件加载 - 这有帮助吗?
  • 我不想回显 $doc->asXML;我想保存新的 galerija.xml
  • 查看asXML($filename_here); 的第一个参数——查看我之前评论中的链接,PHP 手册详细描述了它是如何工作的。我不能做得更好。
【解决方案2】:

这样的事情怎么样:

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>

【讨论】:

  • 有趣,您从 XML 中创建了一个字符串 :) 但是如果 XMl 太长怎么办 :))
  • 我想全部保存在 XML 中,而不是字符串 :)
  • 所以你有一个 xml 文件,你想将数组内容添加到...你想暂时还是永久地这样做? xml 可以是任何长度,它仍然可以工作,只是会变慢。你到底想做什么 - 你的帖子解释得不是很好
【解决方案3】:

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}";
      }
    }

【讨论】:

    猜你喜欢
    • 2016-01-01
    • 2017-01-24
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多