首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在SimpleXMLElement对象中获得值?

如何在SimpleXMLElement对象中获得值?
EN

Stack Overflow用户
提问于 2012-02-27 12:59:09
回答 1查看 4.7K关注 0票数 1

这(var_dump)是一个XMLElement对象的Alexa级别的一个网站。我只想读取排名值(这里是4)。怎么弄到?我想做的是:

打印(字符串)$xml->SD->REACH->秩;//但不工作

代码语言:javascript
复制
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"
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2012-02-27 13:25:11

我在文件里找到了这个:

代码语言:javascript
复制
$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(),看看它是否是您要寻找的。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9465420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档