Link to home
Start Free TrialLog in
Avatar of Fezi
FeziFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Extract XML URL with PHP

Hi,

I am working on a small project and need some help. I am trying to extract the xml url with php in a format that I need to work with. how do I go about doing this.

The XML URL is http://goo.gl/auBjb

The XML URL is coming from a Google Mini that I have set up for searches.

I want to extract the XML data from the URL and create my own XML file that I can use with DHTML GRID (http://www.dhtmlx.com/docs/products/dhtmlxGrid/samples/12_initialization_loading/10_init_grid_xml.html)

If you see the link above, the xml url I am trying to extract is in the following format

<rows>
        <row id="id_here">
            <cell> data</cell>
            <cell> data</cell>
            <cell> data</cell>
            <cell> data</cell>
        </row>
</rows>

Could you please help, thanks.

Regards,
Avatar of Amar Bardoliwala
Amar Bardoliwala
Flag of India image

Hello Fezi,

it seems that you need to exatract data from google and convert it in an array and than create your own xml using that array.

someone will only be able to help you here if you can show how you want to display data from google to grid with some sample data.

regarding extracting data from xml following are some links that can help.

http://www.w3schools.com/php/php_xml_simplexml.asp

http://www.bin-co.com/php/scripts/xml2array/

Hope this will help you.

Thank You.

Amar Bardoliwala.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fezi

ASKER

Hi,

Ray, thanks, i've found the solution, it's something you told me about in previous ticket.

After calling the SimpleXMLElement, add the code below. DHTMLX works differently, even though I had a proper xml fomat, dhtmlx requires it to be a little different, adding a header and content type, etc.
if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) {
    header("Content-type: application/xhtml+xml");
} else {
    header("Content-type: text/xml");
}
echo("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");


if (empty($xml->RES->R)) {
    //echo '<rows><row id="1"><cell>No Results Found</cell></row></rows>';
    $searchterm = $xml->Q;
    $suggestion = $xml->Spelling->Suggestion;

    if ($suggestion != "") {
        echo '<rows><row id="2"><cell><![CDATA["Did you mean <strong><a href="?q=' . strip_tags($suggestion) . '">' . $suggestion . '</a></strong>?"]]></cell></row></rows>';
    }
} else {

    $output = "<rows>";
    foreach ($xml->RES->R as $key) {
        
        $output .= "<row id='" . $key["N"] . "'>";
        $output .= "<cell><![CDATA[" . $key->T . "]]></cell>";
        foreach ($key->MT as $a) {
            $k = $a["N"];
            $v = $a["V"];
            $output .= "<cell>" . $v . "</cell>";
        }
        $output .= "</row>";
    }
    $output .= "</rows>";

    echo $output;
    return true;
}

Open in new window

Avatar of Fezi

ASKER

correct solution, thanks
Thanks for the points.  If you have CDATA in XML, you can still use the SimpleXML class.  You just have to use the NOCDATA flag.
http://us3.php.net/manual/en/function.simplexml-load-string.php#101594

All the best, ~Ray