Milti dimentional XML解析:使用PHP获取值

时间:2013-04-25 16:35:00

标签: php xml-parsing

解析XML后的

var_dump结果是

array(2) {
  ["link"]=>
  object(SimpleXMLElement)#3 (1) {
    [0]=>
    string(24) "http://www.domain.com"
  }
  ["items"]=>
  array(10) {
    [0]=>
    object(SimpleXMLElement)#4 (9) {
      ["title"]=>
      string(32) "The Car Pro Show 4-20-13  Hour 1"
      ["link"]=>
      string(131) "http://www.domain.com/the-car-pro-show-4-20-13-hour-1?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-20-13-hour-1"
      ["comments"]=>
      string(65) "http://www.domain.com/the-car-pro-show-4-20-13-hour-1#comments"
      ["pubDate"]=>
      string(31) "Sat, 20 Apr 2013 19:47:08 +0000"
      ["category"]=>
      object(SimpleXMLElement)#2 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=11118"
      ["description"]=>
      string(89) "… Read moreThe post The Car Pro Show 4-20-13  Hour 1 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#14 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(68) "http://www.domain.com/wp-content/uploads/2013/04/42013-Hour-1.mp3"
          ["length"]=>
          string(8) "37202230"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "4 days ago"
    }
    [1]=>
    object(SimpleXMLElement)#5 (9) {
      ["title"]=>
      string(32) "The Car Pro Show 4-20-13  Hour 2"
      ["link"]=>
      string(131) "http://www.domain.com/the-car-pro-show-4-20-13-hour-2?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-20-13-hour-2"
      ["comments"]=>
      string(65) "http://www.domain.com/the-car-pro-show-4-20-13-hour-2#comments"
      ["pubDate"]=>
      string(31) "Sat, 20 Apr 2013 19:45:46 +0000"
      ["category"]=>
      object(SimpleXMLElement)#14 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=11116"
      ["description"]=>
      string(89) "… Read moreThe post The Car Pro Show 4-20-13  Hour 2 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(68) "http://www.domain.com/wp-content/uploads/2013/04/42013-Hour-2.mp3"
          ["length"]=>
          string(8) "37151773"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "4 days ago"
    }
    [2]=>
    object(SimpleXMLElement)#6 (9) {
      ["title"]=>
      string(32) "The Car Pro Show 4-20-13  Hour 3"
      ["link"]=>
      string(131) "http://www.domain.com/the-car-pro-show-4-20-13-hour-3?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-20-13-hour-3"
      ["comments"]=>
      string(65) "http://www.domain.com/the-car-pro-show-4-20-13-hour-3#comments"
      ["pubDate"]=>
      string(31) "Sat, 20 Apr 2013 19:44:28 +0000"
      ["category"]=>
      object(SimpleXMLElement)#2 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=11114"
      ["description"]=>
      string(89) "… Read moreThe post The Car Pro Show 4-20-13  Hour 3 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#14 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(68) "http://www.domain.com/wp-content/uploads/2013/04/42013-Hour-3.mp3"
          ["length"]=>
          string(8) "37135093"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "4 days ago"
    }
    [3]=>
    object(SimpleXMLElement)#7 (9) {
      ["title"]=>
      string(37) "The Car Pro Show 4-20-13 Network Feed"
      ["link"]=>
      string(143) "http://www.domain.com/the-car-pro-show-4-20-13-network-feed?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-20-13-network-feed"
      ["comments"]=>
      string(71) "http://www.domain.com/the-car-pro-show-4-20-13-network-feed#comments"
      ["pubDate"]=>
      string(31) "Sat, 20 Apr 2013 19:27:54 +0000"
      ["category"]=>
      object(SimpleXMLElement)#14 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=11109"
      ["description"]=>
      string(94) "… Read moreThe post The Car Pro Show 4-20-13 Network Feed appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(61) "http://www.domain.com/wp-content/uploads/2013/04/42013.mp3"
          ["length"]=>
          string(8) "84805120"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "4 days ago"
    }
    [4]=>
    object(SimpleXMLElement)#8 (9) {
      ["title"]=>
      string(32) "The Car Pro Show 4-13-13  Hour 1"
      ["link"]=>
      string(131) "http://www.domain.com/the-car-pro-show-4-13-13-hour-1?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-13-13-hour-1"
      ["comments"]=>
      string(65) "http://www.domain.com/the-car-pro-show-4-13-13-hour-1#comments"
      ["pubDate"]=>
      string(31) "Sat, 13 Apr 2013 19:48:44 +0000"
      ["category"]=>
      object(SimpleXMLElement)#2 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=10968"
      ["description"]=>
      string(89) "… Read moreThe post The Car Pro Show 4-13-13  Hour 1 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#14 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(68) "http://www.domain.com/wp-content/uploads/2013/04/41313-Hour-1.mp3"
          ["length"]=>
          string(8) "37394467"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "1 week ago"
    }
    [5]=>
    object(SimpleXMLElement)#9 (9) {
      ["title"]=>
      string(32) "The Car Pro Show 4-13-13  Hour 2"
      ["link"]=>
      string(131) "http://www.domain.com/the-car-pro-show-4-13-13-hour-2?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-13-13-hour-2"
      ["comments"]=>
      string(65) "http://www.domain.com/the-car-pro-show-4-13-13-hour-2#comments"
      ["pubDate"]=>
      string(31) "Sat, 13 Apr 2013 19:47:18 +0000"
      ["category"]=>
      object(SimpleXMLElement)#14 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=10966"
      ["description"]=>
      string(89) "… Read moreThe post The Car Pro Show 4-13-13  Hour 2 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(68) "http://www.domain.com/wp-content/uploads/2013/04/41313-Hour-2.mp3"
          ["length"]=>
          string(8) "37177627"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "1 week ago"
    }
    [6]=>
    object(SimpleXMLElement)#10 (9) {
      ["title"]=>
      string(32) "The Car Pro Show 4-13-13  Hour 3"
      ["link"]=>
      string(131) "http://www.domain.com/the-car-pro-show-4-13-13-hour-3?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-4-13-13-hour-3"
      ["comments"]=>
      string(65) "http://www.domain.com/the-car-pro-show-4-13-13-hour-3#comments"
      ["pubDate"]=>
      string(31) "Sat, 13 Apr 2013 19:45:55 +0000"
      ["category"]=>
      object(SimpleXMLElement)#2 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=10964"
      ["description"]=>
      string(89) "… Read moreThe post The Car Pro Show 4-13-13  Hour 3 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#14 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(68) "http://www.domain.com/wp-content/uploads/2013/04/41313-Hour-3.mp3"
          ["length"]=>
          string(8) "36899488"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "1 week ago"
    }
    [7]=>
    object(SimpleXMLElement)#11 (9) {
      ["title"]=>
      string(38) "The Car Pro Show Network Feed  4-13-13"
      ["link"]=>
      string(143) "http://www.domain.com/the-car-pro-show-network-feed-4-13-13?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-network-feed-4-13-13"
      ["comments"]=>
      string(71) "http://www.domain.com/the-car-pro-show-network-feed-4-13-13#comments"
      ["pubDate"]=>
      string(31) "Sat, 13 Apr 2013 19:17:07 +0000"
      ["category"]=>
      object(SimpleXMLElement)#14 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=10958"
      ["description"]=>
      string(95) "… Read moreThe post The Car Pro Show Network Feed  4-13-13 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(61) "http://www.domain.com/wp-content/uploads/2013/04/41313.mp3"
          ["length"]=>
          string(8) "83924992"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(10) "1 week ago"
    }
    [8]=>
    object(SimpleXMLElement)#12 (9) {
      ["title"]=>
      string(31) "The Car Pro Show Hour 1  4-6-13"
      ["link"]=>
      string(129) "http://www.domain.com/the-car-pro-show-hour-1-4-6-13?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-hour-1-4-6-13"
      ["comments"]=>
      string(64) "http://www.domain.com/the-car-pro-show-hour-1-4-6-13#comments"
      ["pubDate"]=>
      string(31) "Sat, 06 Apr 2013 19:45:57 +0000"
      ["category"]=>
      object(SimpleXMLElement)#2 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=10824"
      ["description"]=>
      string(88) "… Read moreThe post The Car Pro Show Hour 1  4-6-13 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#14 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(67) "http://www.domain.com/wp-content/uploads/2013/04/4613-Hour-1.mp3"
          ["length"]=>
          string(8) "37239760"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(11) "2 weeks ago"
    }
    [9]=>
    object(SimpleXMLElement)#13 (9) {
      ["title"]=>
      string(31) "The Car Pro Show Hour 2  4-6-13"
      ["link"]=>
      string(129) "http://www.domain.com/the-car-pro-show-hour-2-4-6-13?utm_source=rss&utm_medium=rss&utm_campaign=the-car-pro-show-hour-2-4-6-13"
      ["comments"]=>
      string(64) "http://www.domain.com/the-car-pro-show-hour-2-4-6-13#comments"
      ["pubDate"]=>
      string(31) "Sat, 06 Apr 2013 19:44:42 +0000"
      ["category"]=>
      object(SimpleXMLElement)#14 (0) {
      }
      ["guid"]=>
      string(33) "http://www.domain.com/?p=10822"
      ["description"]=>
      string(88) "… Read moreThe post The Car Pro Show Hour 2  4-6-13 appeared first on Car Pro USA."
      ["enclosure"]=>
      object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=>
        array(3) {
          ["url"]=>
          string(67) "http://www.domain.com/wp-content/uploads/2013/04/4613-Hour-2.mp3"
          ["length"]=>
          string(8) "37202647"
          ["type"]=>
          string(10) "audio/mpeg"
        }
      }
      ["updated"]=>
      string(11) "2 weeks ago"
    }
  }
}

我想获得价值,比如Title,Date& URL。我可以轻松检索标题和日期,PHP代码是:

<?php foreach($entries['items'] as $entry)  { ?>
Title: <?php echo $entry->title; ?>
Date: <?php echo date("M d Y",strtotime($entry->pubDate)); ?>
URL: <?php echo $entry->url ?>
<?php } ?>

但我无法在此获得网址的价值,我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用attributes()方法获取它。

$attrs = $entry->enclosure->attributes();
echo $attrs['url'];