2012-02-27 170 views
1

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

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" 
     } 
    } 
    } 
} 
+0

它是一个DOM节点的属性。看看[文档](http://www.php.net/manual/en/book.simplexml.php)如何访问子孙和属性节点。 – 2012-02-27 13:04:44

+0

@Felix Kling我可以像这样得到它:$ xml-> SD-> REACH [“RANK”] – user774250 2012-02-27 17:21:08

回答

1

我发现这个在docs:

$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->getName()看看它是否是你正在寻找的。

希望这会有所帮助。