繁体   English   中英

从PHP Xpath获取值

[英]Get values from PHP Xpath

我正在使用PHP,并且正在使用simplexml_load_string。 我在获取子节点级别的值时遇到困难。

   $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."\n";
            echo "$key) : ".$video->path."\n";

    }

我如何从\\ video-block \\ video-data-structure \\ video-player \\ media中检索高度和文件的值

以下是video.xml的内容:

  <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>


file as an object you need to use simplexml_load_file . 要将外部文件加载为对象,您需要使用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>\n\n
HEREDOC;
}


产量

0)
video1
http://mycompany.com
405
playvideo.m4v

1)
video2
http://mycompany.com
405
playvideo2.m4v

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM