Amit Patil
Amit Patil

Reputation: 3067

parse complex xml with php simple xml

I want to parse (little critical) xml using php simplexml...But i can aither get attribute or either value...m not able to parse this XML properly

<?xml version="1.0" encoding="UTF-8" ?>
<response uri="/crm/private/xml/Leads/getMyRecords">
    <result>
        <Leads>
            <row no="1">
                <FL val="LEADID">418176000000051001</FL>
                <FL val="SMOWNERID">418176000000047003</FL>
                <FL val="Lead Owner"><![CDATA[Amit Patil]]>
                </FL>
                <FL val="Company"><![CDATA[demo Company]]>
                </FL>
                <FL val="First Name"><![CDATA[Test]]>
                </FL>
                <FL val="Last Name"><![CDATA[Demo]]>
                </FL>
                <FL val="Designation"><![CDATA[Tesing Lead]]>
                </FL>
                <FL val="No of Employees"><![CDATA[0]]>
                </FL>

And this is what i am soo far

$xml = simplexml_load_file($url);
if($xml != null){
   foreach($xml->result->Leads->row as $key=>$row){
       foreach($row->FL as $key=>$val){
           echo $val.",";
       }
   }
}

How can i parse this XML using simplexml or may be something else ??

Upvotes: 0

Views: 833

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

val is an attributeof FL, not the content... so I'm not sure what you're trying to retrieve when you use the ambiguously named $val

As the code you have should already display the content of each element: to retrieve the attributes, use

foreach($xml->result->Leads->row as $key=>$row){
    foreach($row->FL as $key => $value){
        echo $value['val']." => ";
        echo $value."<br />";
    }
}

From your snippet of XML, this gives:

LEADID => 418176000000051001
SMOWNERID => 418176000000047003
Lead Owner => Amit Patil 
Company => demo Company 
First Name => Test 
Last Name => Demo 
Designation => Tesing Lead 
No of Employees => 0 

Upvotes: 4

Related Questions