dqiatvbi61502 2014-03-21 22:35 采纳率: 0%
浏览 31
已采纳

PHP解析XML数据

I have something like this:

  <client type="s" name="root" desc="" protocol="server" protocolext="" au="0" thid="0x15e9190">
     <request="0000" srvid="0000" time="" history="" answered=""></request>
     <times login="2013-04-16T10:59:16+0200" online="7001" idle="0"></times>
     <connection ip="127.0.0.1" port="0">OK</connection>
  </client>

Now, i need to parse this data into a PHP variables, something like this:

 $client_type = s; $name = root; $conn_ip = 127.0.0.1;

...and so on,is there a way to do that?

Tried bash, but it would be much easier if it can be done with php

  • 写回答

4条回答 默认 最新

  • donglugou6652 2014-03-21 22:43
    关注

    PHP has XML support. I like to use SimpleXML.

    $xml = new SimpleXMLElement($xmlString);
    
    $client_type = (string)$xml['type'];
    $name = (string)$xml['name'];
    
    $conn_ip = (string)$xml->connection['ip'];
    

    Easy.

    DEMO: https://eval.in/124545

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里