dongnius85154
dongnius85154
2013-11-18 16:04

PHP将xml输出解析为数组

已采纳

in my project i need a function that create an xml output from alexa data and parse entire result into an array. I have start by first node like this:

<?php
$url="samsung.com";
$xml = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);

$alex2 = array();

//Related links
if($foreach = $xml->xpath('//RLS[@PREFIX="http://"]/value'))
{
 foreach($foreach as $node)
{
$alex2['HREF'] = (string) $node;
}
}

print_r($alex2);

?>

But alex2 array is empty. The xml output in example is like this:

<ALEXA VER="0.9" URL="samsung.com/" HOME="0" AID="=" IDN="samsung.com/">
<RLS PREFIX="http://" more="157">
<RL HREF="halla.com/" TITLE="Halla Engineering and Construction"/>
<RL HREF="daewoo.com/" TITLE="Daewoo"/>
<RL HREF="samsungusa.com/" TITLE="Samsungusa"/>
<RL HREF="samsungmobileusa.com/" TITLE="Welcome to Samsung Mobile USA"/>
<RL HREF="samsungmobile.com/" TITLE=":: Mobile Exciting!! - Samsung Fun Club :"/>
<RL HREF="samsung.nl/" TITLE="New URL"/>
<RL HREF="samsung.hu/" TITLE="Samsung Rt."/>
<RL HREF="samsung.fr/" TITLE="Samsung Electronics France"/>
<RL HREF="samsung.es/" TITLE="Samsung Electronica"/>
<RL HREF="samsung.de/" TITLE="Samsung Electronics GmbH"/>
<RL HREF="www.samsungtelecom.com/" TITLE="www.samsungtelecom.com/"/>
</RLS>
<SD TITLE="A" FLAGS="DMOZ" HOST="samsung.com">
<TITLE TEXT="Samsung Electronics"/>
<CREATED DATE="29-Nov-1994" DAY="29" MONTH="11" YEAR="1994"/>
<PHONE NUMBER="82 2 3415 6436, Fax: 02 3415 6652"/>
<OWNER NAME="Samsung Networks Inc."/>
<EMAIL ADDR="hostmstr@samsung.com"/>
<DOS>
<DO DOMAIN="adoptee.com" TITLE="adoptee.com"/>
<DO DOMAIN="hoammuseum.org" TITLE="hoammuseum.org"/>
<DO DOMAIN="how2lend.com" TITLE="how2lend.com"/>
<DO DOMAIN="samsung.com" TITLE="samsung.com"/>
<DO DOMAIN="samsung.net" TITLE="samsung.net"/>
<DO DOMAIN="samsungelectronics.com" TITLE="samsungelectronics.com"/>
</DOS>
[...]
</ALEXA>

someone have an idea about?

Thanks in advance

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongxu2398 dongxu2398 8年前

    First, <RLS> elements have <RL> children so you have use a nested loop. Then, you have to extract attribute information as HREF is an attribute of the node not a value.

    <?php
    $url="samsung.com";
    $xmlDoc = simplexml_load_file('http://data.alexa.com/data?cli=10&dat=snbamz&url='.$url);
    $alex2 = array();
    
    //Related links
    if($xml = $xmlDoc->xpath('//RLS[@PREFIX="http://"]')) {
      foreach($xml as $node) {
        foreach($node as $n) {
        $a = (array) $n->attributes();
        $alex2['HREF'][] =  $a['@attributes']['HREF'];
        }
      }
    }
    
    点赞 评论 复制链接分享

相关推荐