输出或写入 xml 文件? [英] output or write to a xml file?

查看:27
本文介绍了输出或写入 xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$fp = fopen('data.txt', 'r');

$xml = new SimpleXMLElement('<allproperty></allproperty>');

while ($line = fgetcsv($fp)) {
   if (count($line) < 4) continue; // skip lines that aren't full

   $node = $xml->addChild('aproperty');
   $node->addChild('postcode', $line[0]);
   $node->addChild('price', $line[1]);
   $node->addChild('imagefilename', $line[2]);
   $node->addChild('visits', $line[3]);
}

echo $xml->saveXML();

我用这个脚本把文本文件转换成xml文件,但是我想把它输出到一个文件,我该怎么做这个simpleXML,干杯

im using this script to convert text file into a xml file, but i want to output it to a file, how can i do this simpleXML, cheers

推荐答案

file_put_contents 函数会做到这一点.该函数采用文件名和一些内容并将其保存到文件中.

file_put_contents function would do it. The function take a filename and some content and save it to the file.

因此,重温您的示例,您只需将 echo 语句替换为 file_put_contents.

So retaking your example you would just to replace the echo statement by file_put_contents.

$xml = new SimpleXMLElement('<allproperty></allproperty>');
$fp = fopen('data.txt', 'r');

while ($line = fgetcsv($fp)) {
   if (count($line) < 4) continue; // skip lines that aren't full

   $node = $xml->addChild('aproperty');
   $node->addChild('postcode', $line[0]);
   $node->addChild('price', $line[1]);
   $node->addChild('imagefilename', $line[2]);
   $node->addChild('visits', $line[3]);
}

file_put_contents('data_out.xml',$xml->saveXML());

这篇关于输出或写入 xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆