输出或写入xml文件?

时间:2022-02-23 14:07:40
$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();

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

我使用这个脚本将文本文件转换为xml文件,但是我想将它输出到一个文件中,我如何做这个simpleXML,干杯

2 个解决方案

#1


5  

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

file_put_contents函数将执行它。函数获取文件名和一些内容并将其保存到文件中。

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

因此,重新使用示例,只需将echo语句替换为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());

#2


1  

For the record, you can use asXML() for that. I mean, it's right there in the manual, just read it and your life will get easier. (I assume, perhaps asking * for basic stuff is easier for some)

对于记录,您可以为此使用asXML()。我的意思是,它就在手册里,只要读一下,你的生活就会变得更轻松。(我认为,对于某些人来说,让*来提供一些基本的东西可能更容易)

Also, and this one is more circumstantial, you don't necessarily need to use addChild() for every child. If there is no child of that name, it can be assigned directly using the object property notation:

而且,这个更详细,您不必为每个孩子都使用addChild()。如果没有该名称的子节点,可以使用对象属性表示法直接分配:

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

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

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

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

$xml->asXML('data.xml');

#1


5  

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

file_put_contents函数将执行它。函数获取文件名和一些内容并将其保存到文件中。

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

因此,重新使用示例,只需将echo语句替换为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());

#2


1  

For the record, you can use asXML() for that. I mean, it's right there in the manual, just read it and your life will get easier. (I assume, perhaps asking * for basic stuff is easier for some)

对于记录,您可以为此使用asXML()。我的意思是,它就在手册里,只要读一下,你的生活就会变得更轻松。(我认为,对于某些人来说,让*来提供一些基本的东西可能更容易)

Also, and this one is more circumstantial, you don't necessarily need to use addChild() for every child. If there is no child of that name, it can be assigned directly using the object property notation:

而且,这个更详细,您不必为每个孩子都使用addChild()。如果没有该名称的子节点,可以使用对象属性表示法直接分配:

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

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

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

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

$xml->asXML('data.xml');