We help IT Professionals succeed at work.
Get Started

Simple Parsing XML with PHP

Fezi
Fezi asked
on
354 Views
Last Modified: 2012-05-12
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
Most Valuable Expert 2011
Author of the Year 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE