dsuoedtom207012191 2013-10-10 01:48
浏览 51
已采纳

从PHP Xpath获取值

I am using PHP and am using the simplexml_load_string. I am having difficulties getting the values at the child node level.

   $tmpObject=simplexml_load_string("video.xml");
   $tmpObject=$tmpObject->xpath("//video-block");
   $this->videoObject=array_merge($this->videoObject,$tmpObject);


   foreach($this->videoObject as $key => $video) {
            echo "$key) Name: ".$video->name."
";
            echo "$key) : ".$video->path."
";

    }

How do i retrieve the values for height and file from \video-block\video-data-structure\video-player\media

Here are the video.xml contents:

  <system-video>
    <video-block> 
          <name>video1</name>
          <path>http://mycompany.com</path>
          <dynamic-metadata>
              <name>Navigation Level</name>
              <value>Undefined</value>
          </dynamic-metadata>
          <video-data-structure>
              <video-player>
                  <player>
                      <width>505</width>
                      <height>405</height>
                  </player>
                  <media>
                      <playlistfile/>
                      <file>playvideo.m4v</file>
                      <image>http://mycompany.com/jsmith.jpg</image>
                      <duration/>
                      <start/>
                  </media>
              </video-player>
          </video-data-structure>
      </video-block>
      <video-block>      
          <name>video2</name>
          <path>http://mycompany.com</path>
          <dynamic-metadata>
              <name>Navigation Level</name>
              <value>Undefined</value>
          </dynamic-metadata>
          <video-data-structure>
              <video-player>
                  <player>
                      <width>505</width>
                      <height>405</height>
                  </player>
                  <media>
                      <playlistfile/>
                      <file>playvideo2.m4v</file>
                      <image>http://mycompany.com/Tmatthews.jpg</image>
                      <duration/>
                      <start/>
                  </media>
              </video-player>
          </video-data-structure>
       </video-block>
  </system-video>
  • 写回答

1条回答 默认 最新

  • dongtuan8547 2014-01-11 09:57
    关注


    To load the external  video.xml  file as an object you need to use  simplexml_load_file.


    PHP

    $tmpObject = simplexml_load_file("video.xml");
    $tmpObject = $tmpObject->xpath("//video-block");
    
    foreach($tmpObject as $key => $video)
    {
        $name   = $video->name;
        $path   = $video->path;
        $height = $video->{'video-data-structure'}->{'video-player'}->player->height;
        $file   = $video->{'video-data-structure'}->{'video-player'}->media->file;
    
        echo <<<HEREDOC
    {$key})<br>
    {$name}<br>
    {$path}<br>
    {$height}<br>
    {$file}<br><br>
    
    
    HEREDOC;
    }
    


    Output

    0)
    video1
    http://mycompany.com
    405
    playvideo.m4v
    
    1)
    video2
    http://mycompany.com
    405
    playvideo2.m4v
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站