使用php将文本文件转换为xml?

发布于 2024-10-07 11:51:28 字数 1120 浏览 0 评论 0原文

data.txt

ha15rs,250,home2.gif,2
ha36gs,150,home3.gif,1
ha27se,300,home4.gif,4
ha4678,200,home5.gif,5

我想使用 PHP 的 simplexml 模块将此文本文件转换为 xml?谢谢:))

ps 我是这个

编辑的新手:

<allproperty>
          <aproperty>
                   <postcode></postcode>
                   <price></price>
                   <imagefilename></imagefilename>
                   <visits></visits>
              </aproperty>
              <aproperty>
                   <postcode></postcode>
                   <price></price>
                   <imagefilename></imagefilename>
                   <visits></visits>
              </aproperty>
              <aproperty>
                   <postcode></postcode>
                   <price></price>
                   <imagefilename></imagefilename>
                   <visits></visits>
              </aproperty>
          </allproperty>

data.txt

ha15rs,250,home2.gif,2
ha36gs,150,home3.gif,1
ha27se,300,home4.gif,4
ha4678,200,home5.gif,5

i want convert this textfile into xml using simplexml module using php? thanks :))

p.s. im new to this

EDIT:

<allproperty>
          <aproperty>
                   <postcode></postcode>
                   <price></price>
                   <imagefilename></imagefilename>
                   <visits></visits>
              </aproperty>
              <aproperty>
                   <postcode></postcode>
                   <price></price>
                   <imagefilename></imagefilename>
                   <visits></visits>
              </aproperty>
              <aproperty>
                   <postcode></postcode>
                   <price></price>
                   <imagefilename></imagefilename>
                   <visits></visits>
              </aproperty>
          </allproperty>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫竹語嫣☆ 2024-10-14 11:51:28

我建议您使用 XMLWriter 来代替,因为它最适合这样做(而且它也像 SimpleXML 一样内置):

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

$xml = new XMLWriter;
$xml->openURI('php://output');
$xml->setIndent(true); // makes output cleaner

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

   $xml->startElement('aproperty');
   $xml->writeElement('postcode', $line[0]);
   $xml->writeElement('price', $line[1]);
   $xml->writeElement('imagefilename', $line[2]);
   $xml->writeElement('visits', $line[3]);
   $xml->endElement();
}
$xml->endElement();

当然,如果您希望输出到文件,您可以将 php://output 参数更改为文件名。

I will suggest you to use XMLWriter instead, because it is best suited for that (and it's also built-in like SimpleXML):

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

$xml = new XMLWriter;
$xml->openURI('php://output');
$xml->setIndent(true); // makes output cleaner

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

   $xml->startElement('aproperty');
   $xml->writeElement('postcode', $line[0]);
   $xml->writeElement('price', $line[1]);
   $xml->writeElement('imagefilename', $line[2]);
   $xml->writeElement('visits', $line[3]);
   $xml->endElement();
}
$xml->endElement();

Of course, you can change the php://output argument to a filename if you want it to output to a file.

极度宠爱 2024-10-14 11:51:28

尽管我认为 XMLWriter 最适合该任务(就像我的其他答案),如果你真的想用 SimpleXML 来做,方法如下:

$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();

你会注意到输出不太干净:这是因为 SimpleXML 不允许您自动缩进标签。

Although I think XMLWriter is best suited for that task (like in my other answer), if you really want to do it with SimpleXML, here's how:

$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();

You will notice that the output is not as clean: it's because SimpleXML doesn't allow you to automatically indent tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文