Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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="[email protected]"/>
<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

share|improve this question
    
You are overwriting $alex2['HREF'] in each loop. Try adding print_r($alex2); inside the loop. Perhaps it's getting overwritten in the last loop with empty value... –  Marcell Fülöp Nov 18 '13 at 16:09
    
inside foreach cicle i tried "$alex2['HREF'] .= (string) $node;" but nothing change –  almal Nov 18 '13 at 16:20
    
$xml->xpath('//RLS[@PREFIX="http://"]/value') returns FALSE thus execution is not getting into the if and foreach... –  Marcell Fülöp Nov 18 '13 at 16:26
    
and what is the best method to be able to parse the xml into an array? –  almal Nov 18 '13 at 16:28
    
If you just remove the "/value" bit, it will work. Otherwise, using Simple_Xml_Element::xpath() is a good way of transforming XML data to a PHP array. –  Marcell Fülöp Nov 18 '13 at 16:29
show 2 more comments

1 Answer

up vote 1 down vote accepted

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'];
    }
  }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.