simplexml_load_string-未显示子节点


simplexml_load_string - child nodes not showing

我正在处理一个xml文件,试图将其解析为json格式,然后解码为数组。我主要使用内置的simplexml_load_stringjson_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"
          }
        }

我建议只尝试单独使用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/>
    ";
}

样本输出