dongpangfu6322 2016-11-14 19:00
浏览 91

如何从SimpleXMLElement对象获取值

I am writing that post because i'm trying to solve a problem which seems to be stupid but unfortunately i cannot make it. I have an XML file (link) which i'm parsing with simplexml_load_string($link) as below:

$response_xml_data = file_get_contents($link);
$xml = simplexml_load_string($response_xml_data);
foreach ($xml->item as $licenseElement) {
    $title = $licenseElement->title;
    echo $title;
}

I need to get value of title and link but no any result.

I try to print $link and show me this :

SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [version] => 2.0
            )

        [channel] => SimpleXMLElement Object
            (
                [title] => GamePlay
                [link] => https://example.com/
                [description] => The latest blog posts from example.com.
                [language] => en-us
                [pubDate] => Mon, 14 Nov 2016 18:50:50 +0000
                [lastBuildDate] => Mon, 14 Nov 2016 18:50:50 +0000
                [managingEditor] => info@example.com
                [item] => SimpleXMLElement Object
                    (
                        [title] => IO Interactive 
                        [link] => http://example.com/?p=258
                    )

            )

    )

UPDATE: When try to var_dump($xml); before loop :

object(SimpleXMLElement)#2568 (2) {
  ["@attributes"]=>
  array(1) {
    ["version"]=>
    string(3) "2.0"
  }
  ["channel"]=>
  object(SimpleXMLElement)#2571 (8) {
    ["title"]=>
    string(8) "Gameplay"
    ["link"]=>
    string(21) "https://example.com/"
    ["description"]=>
    string(40) "The latest blog posts from example.com."
    ["language"]=>
    string(5) "en-us"
    ["pubDate"]=>
    string(31) "Mon, 14 Nov 2016 19:08:33 +0000"
    ["lastBuildDate"]=>
    string(31) "Mon, 14 Nov 2016 19:08:33 +0000"
    ["managingEditor"]=>
    string(17) "info@example.com"
    ["item"]=>
    object(SimpleXMLElement)#2574 (2) {
      ["title"]=>
      string(76) "IO Interactive "
      ["link"]=>
      string(217) "http://example.com/?p=258"
    }
  }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度