akash dwivedi
akash dwivedi

Reputation: 77

parsing of xml in php

<aws:TrafficHistoryResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
<aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11">
<aws:OperationRequest>
<aws:RequestId>0effb937-80d1-431d-80c2-95a883de1040</aws:RequestId>
</aws:OperationRequest>
<aws:TrafficHistoryResult>
<aws:Alexa>
<aws:TrafficHistory>
<aws:Range>2</aws:Range>
<aws:Site>http://xeler8.com/</aws:Site>
<aws:Start>2016-03-01</aws:Start>
<aws:HistoricalData>
<aws:Data>
<aws:Date>2016-03-01</aws:Date>
<aws:PageViews>
<aws:PerMillion>0.06</aws:PerMillion>
<aws:PerUser>1.00</aws:PerUser>
</aws:PageViews>
<aws:Rank>339328</aws:Rank>
<aws:Reach>
<aws:PerMillion>5</aws:PerMillion>
</aws:Reach>
</aws:Data>
<aws:Data>
<aws:Date>2016-03-02</aws:Date>
<aws:PageViews>
<aws:PerMillion>0.03</aws:PerMillion>
<aws:PerUser>1.00</aws:PerUser>
</aws:PageViews>
<aws:Rank>686679</aws:Rank>
<aws:Reach>
<aws:PerMillion>2</aws:PerMillion>
</aws:Reach>
</aws:Data>
</aws:HistoricalData>
</aws:TrafficHistory>
</aws:Alexa>
</aws:TrafficHistoryResult>
<aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
<aws:StatusCode>Success</aws:StatusCode>
</aws:ResponseStatus>
</aws:Response>

this is response I want to parse in php want to grap the value of Rank Node

$xml = new SimpleXMLElement($response,null,false,
                            'http://awis.amazonaws.com/doc/2005-07-11');
echo $xml->TrafficHistoryResponse->Response->TrafficHistoryResult->Alexa->TrafficHistory->HistoricalData->Data->Rank;

using this but not able to get he data getting error as

PHP Notice: Trying to get property of non-object in /home/ras-al-ghul/Downloads/urlinfo.php on line 100

Upvotes: 1

Views: 80

Answers (2)

Matthias Wiehl
Matthias Wiehl

Reputation: 1998

First off, your XML document is missing the closing aws:TrafficHistoryResponse tag at the end.

You can retrieve the text values of the Rank nodes as follows:

$xml = new SimpleXMLElement($response);
$xml->registerXPathNamespace('a', 'http://awis.amazonaws.com/doc/2005-07-11');

foreach ($xml->xpath("//a:Rank") as $rank) {
    echo $rank . PHP_EOL;
}

Output:

339328
686679

Upvotes: 1

MaximeK
MaximeK

Reputation: 2071

Your XML file is not valid add these 2 line in the end of the doc :

</aws:Response>
</aws:TrafficHistoryResponse>

The trick is to do $response = str_replace("aws:","",$response); to avoid the problem of namespace :)

And here the code in full :

$response ='<aws:TrafficHistoryResponse xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
        <aws:Response xmlns:aws="http://awis.amazonaws.com/doc/2005-07-11">
            <aws:OperationRequest>
                <aws:RequestId>0effb937-80d1-431d-80c2-95a883de1040</aws:RequestId>
            </aws:OperationRequest>
            <aws:TrafficHistoryResult>
                <aws:Alexa>
                    <aws:TrafficHistory>
                        <aws:Range>2</aws:Range>
                        <aws:Site>http://xeler8.com/</aws:Site>
                        <aws:Start>2016-03-01</aws:Start>
                        <aws:HistoricalData>
                            <aws:Data>
                                <aws:Date>2016-03-01</aws:Date>
                                <aws:PageViews>
                                    <aws:PerMillion>0.06</aws:PerMillion>
                                    <aws:PerUser>1.00</aws:PerUser>
                                </aws:PageViews>
                                <aws:Rank>339328</aws:Rank>
                                <aws:Reach>
                                    <aws:PerMillion>5</aws:PerMillion>
                                 </aws:Reach>
                             </aws:Data>
                            <aws:Data>
                                <aws:Date>2016-03-02</aws:Date>
                                <aws:PageViews>
                                    <aws:PerMillion>0.03</aws:PerMillion>
                                    <aws:PerUser>1.00</aws:PerUser>
                                </aws:PageViews>
                                <aws:Rank>686679</aws:Rank>
                                <aws:Reach>
                                    <aws:PerMillion>2</aws:PerMillion>
                                </aws:Reach>
                            </aws:Data>
                        </aws:HistoricalData>
                    </aws:TrafficHistory>
                </aws:Alexa>
            </aws:TrafficHistoryResult>
            <aws:ResponseStatus xmlns:aws="http://alexa.amazonaws.com/doc/2005-10-05/">
            <aws:StatusCode>Success</aws:StatusCode>
            </aws:ResponseStatus>
        </aws:Response>
</aws:TrafficHistoryResponse>';

$response = str_replace("aws:","",$response);
try { 
    $xml = new SimpleXmlElement($response);
} catch (Exception $e) { 
    echo $e; 
}
$data = $xml->xpath("//TrafficHistoryResponse/Response/TrafficHistoryResult/Alexa/TrafficHistory/HistoricalData/Data[last()]");
print_r($data[0]->Rank);

Upvotes: 0

Related Questions