dtbiszu7724 2015-10-02 08:37
浏览 44
已采纳

PHP字符串到SimpleXML

my question is really simple and it's just out of curiosity.

I have a string formatted as XML <node><child attr="value">data</child><child attr="value">data</child></node>... and I need to convert it to SimpleXML. I don't have any issue, I know how to do it and how to handle it, I just wanna know what's the difference between

$xml_data = new SimpleXMLElement($string);

and

$xml_data = simplexml_load_string($string);

I also would like to know if I am supposed to save the string as a well formatted XML file, with the header <?xml version=”1.0″ encoding=”utf-8″?> to avoid any issue. By the way I only need the data to be handled as an array and I will never save it as a file so keep it in mind when you answer

Thanks in advance for the answers

  • 写回答

2条回答 默认 最新

  • dtest84004 2015-10-02 08:54
    关注

    It's just 2 different ways of doing it, the only difference is that the __construct new SimpleXMLElement takes an additional argument that allows you to load file from a uri which is the same behavior as using simplexml_load_file() instead of _string

    About the header it depends on the version of XML you are using,

    1.0 is optional, 1.1 is mandatory, have a look at Does a valid XML file require an xml declaration?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)