duanqiao8925 2016-07-27 06:18
浏览 31
已采纳

将标签从xml文件读入php数组

I am trying to display a list of all the tags in a XML file. But I can't seem to do it. [

This is the XML I am tying to read the tags from: http://i.stack.imgur.com/9bYHi.png

If someone knows a way to keep the tree struckture of the xml file and trasfer it into an array. That would be really helpfull.

My question is different because I don't know the contents of the xml file. My image is just an example. I am looking for a solution that reads all the tags from an xml file and also keeps the tree structure when putting it in an array.

  • 写回答

1条回答 默认 最新

  • doudaochu1699 2016-07-27 06:25
    关注

    I always use xml_parser_create, so you could do the following

    $yourxmlfile = "pathto/yourfilename.xml";
    $xmldata = file_get_contents($yourxmlfile);
    $p = xml_parser_create();
    xml_parse_into_struct($p, $xmldata, $vals, $index);
    xml_parser_free($p);
    
    print_r($vals);
    

    that should print the xml structure as an array, see http://php.net/manual/en/function.xml-parse-into-struct.php for more info about it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部