dongyi4420 2014-03-20 05:51
浏览 76
已采纳

使用PHP从XML文档中的属性中提取信息

I was unable to find an answer to my question after browsing some promising titles so I figured I would try my luck!

I'm attempting to extract information from XML files generated from a back-end we use at work. The information is not formatted like a normal XML document, so it's been a little difficult to extract what I need. I'm hoping that I can eventually put this information in a table, but I'll be happy if I can at least extract the values.

An example string is as follows:

<Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/>

And I'm trying to get:

Event Name:
Venue Name:
Event Date:
Direct Link:

I'm assuming I need to use substr, but I've never dealt with a string like this.

If you take a moment to look at this, I would greatly appreciate it!

  • 写回答

4条回答 默认 最新

  • dongyin2885 2014-03-20 06:04
    关注

    simplexml_load_string() makes working with XML easy for basic tasks like this:

    <?php
    $event= simplexml_load_string('<Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/>');
    ?>
    
    Event Name: <?= $event['Name']; ?><br>
    Venue Name: <?= $event['VenueName']; ?><br>
    Event Date: <?= $event['EventDate']; ?><br>
    Direct Link: <?= $event['DirectLink']; ?>
    

    See it in action

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

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部