使用php将文本文件转换为xml?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用 XMLWriter 来代替,因为它最适合这样做(而且它也像 SimpleXML 一样内置):
当然,如果您希望输出到文件,您可以将
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):
Of course, you can change the
php://output
argument to a filename if you want it to output to a file.尽管我认为 XMLWriter 最适合该任务(就像我的其他答案),如果你真的想用 SimpleXML 来做,方法如下:
你会注意到输出不太干净:这是因为 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:
You will notice that the output is not as clean: it's because SimpleXML doesn't allow you to automatically indent tags.