We help IT Professionals succeed at work.

Simple Parsing XML with PHP

Fezi
Fezi asked
on
Hi Experts,

I need help with something that I am working on. I have an XML file, and I am trying to return the results into HTML format, using PHP (SimpleXML). Below is some of my code to show you what I am doing.

I want to get the Attributes under the <PageMap> and<DataObject> tag, how do I get to show the attributes from the PageMap > DataObject ?

All help appreciated.

Thanks.

Regards,
Faraz Shafiq
XML FILE...

<RES>
<R N="6">
    <U></U>
    <UE></UE>
    <T>5g Klebfix Super Glue, 7811. test</T>
    <RK>0</RK>
    <S>Wurth, Dated: June 14 2011, Reg: CHIP.</S>
    <LANG>en</LANG>
    <Label>_cse_rlribpztlus</Label>
    <PageMap>
        <DataObject type="document">
            <Attribute name="title">5g Klebfix Super Glue, 7811</Attribute>
            <Attribute name="author">Fezi</Attribute>
            <Attribute name="description">des - Wurth, Dated: June 14 2011, Reg: CHIP</Attribute>
        </DataObject>
    </PageMap>
    <HAS>
        <L/>
        <C CID="MXP-AH12OxUJ" SZ="2k"/>
        <RT/>
    </HAS>
    <ELIGIBLE_FOR_VISUAL_SNIPPET/>
</R>
</RES>

PHP FUNCTION...

function writeSearchResults($xml) {
       
        if(empty($xml->RES->R)) {
            return false;
        }

        foreach ($xml->RES->R as $key) {
            $output .= '<p class="wrapper"></p>';
            $output .= '<p class="iconwrapper"><a href=""><img src="sds_icon.png" alt="view" /></a><br/><a href=""><img src="add_icon.png" alt="add" /></a></p>';
            $output .= '<p class="search-result"><a href="'.$key->U.'">'.$key->T.'</a><br/>';
            $output .= $key->S.'<br />';
            //$output .= '<span>'.$key->U.'</span></p>';
            $output .= '</p>';
        }

        echo $output;
        return true;
    }

Open in new window

Comment
Watch Question

foreach( $key->PageMap->DataObject->attribute as $att ) {
   $output .= '<p class="dataobjectattributes">'. $att .'</p>';
}

Open in new window


I'm not sure if that's what you're asking.

Author

Commented:
How do I get the child nodes attributes?

Author

Commented:
I've already tried that code, It doesn't seem to work.

How do i get the attribute values to show?
Most Valuable Expert 2011
Top Expert 2016

Commented:
I'll try to show you how to extract this information, but please be aware that "attribute" is a term of art in HTML and XML, and you might not want to use it as the name of a tag - could be confusing.  Here is what I am talking about.  This is an HTML tag with no attributes:

<form>

This is an HTML tag with the "method" attribute:

<form method="post">

In other words, the attribute is part of the tag itself, not a separate control or tag.
Most Valuable Expert 2011
Top Expert 2016

Commented:
See http://www.laprbass.com/RAY_temp_fezi.php
Outputs:
title: 5g Klebfix Super Glue, 7811
author: Fezi
description: des - Wurth, Dated: June 14 2011, Reg: CHIP

HTH, ~Ray
<?php // RAY_temp_fezi.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$xml = <<<XML
<RES>
<R N="6">
    <U></U>
    <UE></UE>
    <T>5g Klebfix Super Glue, 7811. test</T>
    <RK>0</RK>
    <S>Wurth, Dated: June 14 2011, Reg: CHIP.</S>
    <LANG>en</LANG>
    <Label>_cse_rlribpztlus</Label>
    <PageMap>
        <DataObject type="document">
            <Attribute name="title">5g Klebfix Super Glue, 7811</Attribute>
            <Attribute name="author">Fezi</Attribute>
            <Attribute name="description">des - Wurth, Dated: June 14 2011, Reg: CHIP</Attribute>
        </DataObject>
    </PageMap>
    <HAS>
        <L/>
        <C CID="MXP-AH12OxUJ" SZ="2k"/>
        <RT/>
    </HAS>
    <ELIGIBLE_FOR_VISUAL_SNIPPET/>
</R>
</RES>
XML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS EACH OF THE "Attribute" ELEMENTS
foreach ($obj->R->PageMap->DataObject->Attribute as $a)
{
    $key = (string)$a["name"];
    $val = (string)$a[0];
    echo PHP_EOL . "<b>$key:</b> $val";
}

Open in new window

Author

Commented:
Hi Ray_Paseur,

Thanks, I've tried in a test page, I can't seem to get it working with the xml file. See code below, what am i doing wrong?
<?php // RAY_temp_fezi.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$xmlfile = 'http://www.google.com/search?cx=011421384147983897002:rlribpztlus&client=google-csbe&start=0&num=7&output=xml_no_dtd&q=faraz';
$xml = new SimpleXMLElement(file_get_contents($xmlfile));

// ACTIVATE THIS TO VISUALIZE THE OBJECT
//var_dump($xml);

// USE AN ITERATOR TO ACCESS EACH OF THE "Attribute" ELEMENTS
foreach ($xml->R->PageMap->DataObject->Attribute as $a)
{
    $key = (string)$a["name"];
    $val = (string)$a[0];
    echo PHP_EOL . "<b>$key:</b> $val";
}
?>

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Activate the var_dump() statement so you can see the data.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Try this.  It seems to work for me.

Best regards, ~Ray
<?php // RAY_temp_fezi.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$xml = file_get_contents("http://www.google.com/search?cx=011421384147983897002:rlribpztlus&client=google-csbe&start=0&num=7&output=xml_no_dtd&q=faraz");

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS EACH OF THE "Attribute" ELEMENTS
foreach ($obj->RES->R as $r)
{
    $thing = (string)$r->T;
    echo PHP_EOL . $thing;
}

Open in new window

Author

Commented:
Hi,

I've already got it working for R, but I want to be able to tap under PageMap > DataObject > Attributes

Your first example, is pretty much what I want to be able to do. Anyway of getting it working with this XML file?

Author

Commented:
Any Luck?
Most Valuable Expert 2011
Top Expert 2016
Commented:
Sure, here you go.
http://www.laprbass.com/RAY_temp_fezi.php

Going forward, it's probably best to post the actual test data with the question, instead of changing the test data after the question has been answered once.  It's easy to explain the methods we use for things like this, but it wastes your time when we have to keep rewriting the code.

Does this code snippet make sense to you?
<?php // RAY_temp_fezi.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$xml = file_get_contents("http://www.google.com/search?cx=011421384147983897002:rlribpztlus&client=google-csbe&start=0&num=7&output=xml_no_dtd&q=faraz");

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS EACH OF THE "Attribute" ELEMENTS
foreach ($obj->RES->R as $r)
{
    $n = $r["N"];
    $t = (string)$r->T;
    echo PHP_EOL . htmlentities("$n $t");
    if (isset($r->PageMap->DataObject))
    {
        foreach ($r->PageMap->DataObject as $o)
        {
            foreach ($o->Attribute as $a)
            {
                $k = $a["name"];
                $v = $a["value"];
                echo PHP_EOL . "<b>" . htmlentities("$k = $v") . "</b>";
            }
        }
    }
}

Open in new window

Author

Commented:
Hi Ray,

My apologies.

Thank you for the code, will this loop through each attribute, so in other words, if other results in the XML contained <pageMap>.....</pageMap> information, it will display them?
Most Valuable Expert 2011
Top Expert 2016

Commented:
When you say "other results in the XML" it does not really capture the structure of the XML.  See line 20 of the previous snippet.  If the $r object contains a data element at $r->PageMap->DataObject, it will find it and display it.  Note that pageMap is not the same as PageMap -- these are case-sensitive like variable names.

Author

Commented:
Hi Ray...

I tried the code and modified it a little, it works perfectly, thank you so much. Your awesome!
Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for your kind words.  Using var_dump($obj) really makes it easy to see what the data looks like, and this determines the code you need to use.

All the best, ~Ray

Author

Commented:
Perfect solution. Thank you.