duanliao6077
duanliao6077
2015-02-01 23:21

通过xmlwriter PHP在xml文件中写入数据

Hello my script is creating xml file with two elements in it but im not able to adding next element(name = item) to created xml file. can anyone help me?? :) can i load file enter items add one item and save file???

<?php
  $content = file_get_contents('http://evidujem.sk');
  preg_match('#<br /><b> (.*?) užívateľov</b>#', $content, $match);
  $pocet = $match[1];

  $writer = new XMLWriter();
  $writer->openMemory();
  $writer->setIndent(true);
  $writer->setIndentString('  ');
  $writer->startDocument('1.0', 'UTF-8');
  $writer->startElement('items');
  $writer->startElement('item');
  $writer->writeAttribute('aktualny_datum', date('j.n. Y'));
  $writer->text('pocet registrovanych:' . $pocet);
  $writer->endElement();
  $writer->endElement();
  $writer->endDocument();

  $filename = 'pocet.xml';
  $file = $writer->outputMemory();
  file_put_contents($filename,$file);

  ?>

output in the xml file shoul be:

<items><item date="aktualny datum">pocet registrovanych</item>
       <item>...</item>
</items>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答