simplexml_load_string - 未显示的子节点

时间:2015-02-18 06:10:26

标签: php xml

我正在使用xml文件,我尝试将其解析为json格式,然后解码为数组。我主要使用内置的simplexml_load_string然后json_encode完成了这项工作。问题是在调用simplexml_load_string时,xml未完全保留。似乎video的子节点显示为object(stdClass)。我怎样才能获得xml文件的所有值?链接到XML

代码:

$xml = simplexml_load_string( file_get_contents('http://foxsoccer2go.mobilefeeds.performgroup.com/fox/api/videos.xml/channel/home') );
$json = json_encode($xml);

结果:

["results"]=>
  object(stdClass)#183 (4) {
    ["previousPage"]=>
    object(stdClass)#184 (1) {
      ["@attributes"]=>
      object(stdClass)#185 (1) {
        ["exists"]=>
        string(5) "false"
      }
    }
    ["nextPage"]=>
    string(1) "2"
    ["total"]=>
    string(2) "40"
    ["resultList"]=>
    object(stdClass)#186 (1) {
      ["video"]=>
      array(20) {
        [0]=>
        object(stdClass)#187 (7) {
          ["@attributes"]=>
          object(stdClass)#188 (2) {
            ["id"]=>
            string(7) "2329124"
            ["type"]=>
            string(3) "960"
          }
          ["description"]=>
          object(stdClass)#189 (0) {
          }
          ["created"]=>
          string(25) "2015-02-18 04:04:52 +0000"
          ["duration"]=>
          string(2) "86"
          ["images"]=>
          object(stdClass)#190 (2) {
            ["image"]=>
            object(stdClass)#191 (1) {
              ["@attributes"]=>
              object(stdClass)#192 (3) {
                ["id"]=>
                string(8) "13503818"
                ["width"]=>
                string(3) "100"
                ["height"]=>
                string(3) "100"
              }
            }
            ["thumbnail"]=>
            object(stdClass)#193 (1) {
              ["@attributes"]=>
              object(stdClass)#194 (3) {
                ["id"]=>
                string(8) "13503819"
                ["width"]=>
                string(3) "372"
                ["height"]=>
                string(3) "210"
              }
            }
          }
          ["videoFiles"]=>
          object(stdClass)#195 (1) {
            ["file"]=>
            object(stdClass)#196 (1) {
              ["@attributes"]=>
              object(stdClass)#197 (3) {
                ["id"]=>
                string(8) "14704560"
                ["formatId"]=>
                string(3) "400"
                ["uploaded"]=>
                string(4) "true"
              }
            }
          }
          ["categories"]=>
          object(stdClass)#198 (1) {
            ["category"]=>
            string(21) "UEFA Champions League"
          }
        }

1 个答案:

答案 0 :(得分:1)

我建议只尝试使用SimpleXML解析这些值并坚持使用它。只需正确访问这些属性。对于那些已包含字符数据的节点,将其转换为(string)

$xml = simplexml_load_string( file_get_contents('http://foxsoccer2go.mobilefeeds.performgroup.com/fox/api/videos.xml/channel/home'));
foreach($xml->results->resultList->video as $video) {
    $description = (string) $video->description;
    $created = $video->created;
    $duration = $video->duration;
    $image = $video->images->image;
    $thumbnail = (string) $video->images->image;
    $video_file = (string) $video->videoFiles->file;
    $categories = (string) $video->categories->category;

    echo "
    Description: $description <br/>
    Created: $created <br/>
    Duration: $duration <br/>
    Categories: $categories <br/>
    <hr/>
    ";
}

Sample Output