1

I have a String which gets its value dynamically from other server. Value of string is

$xmloutput = '<response uri="/crm/private/xml/Leads/getRecordById">
    <result>
        <Leads>
            <row no="1">
                <FL val="LEADID">131</FL>
                <FL val="SMOWNERID">20001</FL>
                <FL val="Lead Owner"><![CDATA[Aaron]]></FL>
                <FL val="First Name"><![CDATA[Carol]]></FL>
                <FL val="Last Name"><![CDATA[Custer]]></FL>
                <FL val="Email"><![CDATA[[email protected]]]></FL>
            </row>
            <row no="2">
                <FL val="LEADID">2070</FL>
                <FL val="SMOWNERID">20001</FL>
                <FL val="Lead Owner"><![CDATA[Aaron]]></FL>
                <FL val="Last Name"><![CDATA[Florence, SC]]></FL>
            </row>
        </Leads>
    </result>
</response>

My Question is, generally we use $xml = simplexml_load_file("test1.xml"); to load XML files, but here in my requirement it is a String, how can i read this String value and extract the child node and its value? Example:

<FL val="LEADID">131</FL> // its key is LEADID and value is 131
<FL val="First Name"><![CDATA[Carol]]></FL> // its key is First Name and value is Carol

Is there a way to put this into Array? so that its easy for me to print out its node value?

1

1 Answer 1

2

Use simplexml_load_string() that is for reading xml from string. Then loop through row elements and in loop, loop through FL elements and then get text content and attribute of element.

$xml = simplexml_load_string($string); 
foreach ($xml->Leads->row as $row) {
    foreach ($row->FL as $fl) {
        echo "{$fl} => {$fl['val']}<br>";
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting following Warning Message for the this line foreach ($xml->Leads->row as $row) Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\zohoclient\xmlToHtml.php on line 50
@A.T.M.Hasan It's warning and you can turn off it using error_reporting(0)
It is not giving me the output at the same time!
@Mohammad It's not a warning you should turn off. It means the thing you're trying to loop over isn't actually an array, which means you have a bug in the code.
Thanks a lot, buddy! foreach ($xml->result->Leads->row as $row) worked for me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.