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

时间:2015-02-01 23:21:38

标签: php xmlwriter

Hello我的脚本正在创建包含两个元素的xml文件,但我无法将下一个元素(name = item)添加到创建的xml文件中。谁能帮我?? :)我可以加载文件输入项目添加一个项目并保存文件???

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

  ?>

xml文件中的输出应为:

<items><item date="aktualny datum">pocet registrovanych</item>
       <item>...</item>
</items>

0 个答案:

没有答案