【问题标题】:How to get value in SimpleXMLElement object?如何在 SimpleXMLElement 对象中获取值?
【发布时间】:2012-07-06 21:57:16
【问题描述】:

这个 (var_dump) 是一个网站的 Alexa 排名的 XMLElement 对象。我想要的只是读取 RANK 值(这里是 4)。如何得到它? 我正在尝试:

print (string) $xml->SD->REACH->RANK; //但是不行

object(SimpleXMLElement)#1 (4) {
  ["@attributes"]=>
  array(4) {
    ["VER"]=>
    string(3) "0.9"
    ["URL"]=>
    string(10) "yahoo.com/"
    ["HOME"]=>
    string(1) "0"
    ["AID"]=>
    string(1) "="
  }
  ["KEYWORDS"]=>
  object(SimpleXMLElement)#2 (1) {
    ["KEYWORD"]=>
    array(2) {
      [0]=>
      object(SimpleXMLElement)#12 (1) {
        ["@attributes"]=>
        array(1) {
          ["VAL"]=>
          string(10) "On the Web"
        }
      }
      [1]=>
      object(SimpleXMLElement)#4 (1) {
        ["@attributes"]=>
        array(1) {
          ["VAL"]=>
          string(11) "Web Portals"
        }
      }
    }
  }
  ["DMOZ"]=>
  object(SimpleXMLElement)#14 (1) {
    ["SITE"]=>
    object(SimpleXMLElement)#7 (2) {
      ["@attributes"]=>
      array(3) {
        ["BASE"]=>
        string(10) "yahoo.com/"
        ["TITLE"]=>
        string(6) "Yahoo!"
        ["DESC"]=>
        string(133) "A major internet portal and service provider offering search results, customizable content, chatrooms, free e-mail, clubs, and pager."
      }
      ["CATS"]=>
      object(SimpleXMLElement)#8 (1) {
        ["CAT"]=>
        array(3) {
          [0]=>
          object(SimpleXMLElement)#11 (1) {
            ["@attributes"]=>
            array(3) {
              ["ID"]=>
              string(45) "Top/Computers/Internet/On_the_Web/Web_Portals"
              ["TITLE"]=>
              string(22) "On the Web/Web Portals"
              ["CID"]=>
              string(6) "375197"
            }
          }
          [1]=>
          object(SimpleXMLElement)#10 (1) {
            ["@attributes"]=>
            array(3) {
              ["ID"]=>
              string(34) "Top/Computers/Companies/Yahoo_Inc."
              ["TITLE"]=>
              string(20) "Companies/Yahoo Inc."
              ["CID"]=>
              string(6) "376283"
            }
          }
          [2]=>
          object(SimpleXMLElement)#9 (1) {
            ["@attributes"]=>
            array(3) {
              ["ID"]=>
              string(118) "Top/Regional/North_America/United_States/California/Localities/S/Sunnyvale/Business_and_Economy/Computers_and_Internet"
              ["TITLE"]=>
              string(43) "Business and Economy/Computers and Internet"
              ["CID"]=>
              string(6) "627776"
            }
          }
        }
      }
    }
  }
  ["SD"]=>
  object(SimpleXMLElement)#13 (3) {
    ["POPULARITY"]=>
    object(SimpleXMLElement)#3 (1) {
      ["@attributes"]=>
      array(2) {
        ["URL"]=>
        string(10) "yahoo.com/"
        ["TEXT"]=>
        string(1) "4"
      }
    }
    ["REACH"]=>
    object(SimpleXMLElement)#6 (1) {
      ["@attributes"]=>
      array(1) {
        ["RANK"]=>
        string(1) "4"
      }
    }
    ["RANK"]=>
    object(SimpleXMLElement)#5 (1) {
      ["@attributes"]=>
      array(1) {
        ["DELTA"]=>
        string(2) "+0"
      }
    }
  }
}

【问题讨论】:

  • 这是一个DOM节点的属性。看看the documentation如何访问后代和属性节点。
  • @Felix Kling 我可以像这样得到它:$xml->SD->REACH["RANK"]

标签: php


【解决方案1】:

我在文档中找到了这个:

$XML =
'<data>
   <USERS>
      <ID>alang</ID>
      <NAME>Alan Gruskoff</NAME>
      <ROLE>mgr</ROLE>
   </USERS>
</data>';

$xmlObject = new SimpleXMLElement($XML);

$node = $xmlObject->children();

echo  $node[0]->ID;
echo  $node[0]->NAME;
echo  $node[0]->ROLE;

======

我想您需要遍历节点的 children(),然后遍历 $node-&gt;getName() 以查看它是否是您要查找的节点。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2020-01-04
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多