dongyi4420 2014-03-20 13: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 14: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条)

报告相同问题?

悬赏问题

  • ¥15 Delphi image图片缩放问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥15 初学者用plt报错,求解答
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行