Vishal
Vishal

Reputation: 13

How can I featch result from xml through php?

I have following XML String. I want to fetch particular result from this XML which is dynamically changed through selection.

<response uri="/crm/private/xml/Campaigns/getMyRecords">
  <result>
    <Campaigns>
      <row no="1">
        <FL val="CAMPAIGNID">536661000000249003</FL>
        <FL val="SMOWNERID">536661000000051003</FL>
        <FL val="Campaign Owner">
          <![CDATA[gunjan.sharma]]>
        </FL>
        <FL val="Campaign Name">
          <![CDATA[Test Campaign]]>
        </FL>
        <FL val="Status">
          <![CDATA[Active]]>
        </FL>
        <FL val="Expected Revenue">
          <![CDATA[0]]>
        </FL>
        <FL val="Budgeted Cost">
          <![CDATA[0]]>
        </FL>
        <FL val="Actual Cost">
          <![CDATA[0]]>
        </FL>
        <FL val="SMCREATORID">536661000000051003</FL>
        <FL val="Created By">
          <![CDATA[gunjan.sharma]]>
        </FL>
        <FL val="MODIFIEDBY">536661000000051003</FL>
        <FL val="Modified By">
          <![CDATA[gunjan.sharma]]>
        </FL>
        <FL val="Created Time">
          <![CDATA[2012-05-14 20:33:56]]>
        </FL>
        <FL val="Modified Time">
          <![CDATA[2012-05-14 20:33:56]]>
        </FL>
        <FL val="Campaign No">
          <![CDATA[TCam002]]>
        </FL>
        <FL val="Campaigns Source">
          <![CDATA[Share Junction]]>
        </FL>
      </row>
      <row no="2">
        <FL val="CAMPAIGNID">536661000000198011</FL>
        <FL val="SMOWNERID">536661000000051003</FL>
        <FL val="Campaign Owner">
          <![CDATA[gunjan.sharma]]>
        </FL>
        <FL val="Campaign Name">
          <![CDATA[Share Junction]]>
        </FL>
        <FL val="Status">
          <![CDATA[Active]]>
        </FL>
        <FL val="Expected Revenue">
          <![CDATA[0]]>
        </FL>
        <FL val="Budgeted Cost">
          <![CDATA[0]]>
        </FL>
        <FL val="Actual Cost">
          <![CDATA[0]]>
        </FL>
        <FL val="SMCREATORID">536661000000051003</FL>
        <FL val="Created By">
          <![CDATA[gunjan.sharma]]>
        </FL>
        <FL val="MODIFIEDBY">536661000000051003</FL>
        <FL val="Modified By">
          <![CDATA[gunjan.sharma]]>
        </FL>
        <FL val="Created Time">
          <![CDATA[2012-05-07 21:43:33]]>
        </FL>
        <FL val="Modified Time">
          <![CDATA[2012-05-07 21:43:33]]>
        </FL>
        <FL val="Campaign No">
          <![CDATA[CAM01]]>
        </FL>
        <FL val="Campaigns Source">
          <![CDATA[Share Junction]]>
        </FL>
      </row>
    </Campaigns>
  </result>
</response>

I want to get row where Campaign No = CAM01. How can I?


I just want to run this type of query

select CAMPAIGNID from Campaigns where Campaign No = '$val'


or convert it in array


is it possible?

Upvotes: 0

Views: 64

Answers (2)

Deeps23
Deeps23

Reputation: 51

Here's what to do, in few steps:

  1. Load the XML file
  2. Get the name of the first element
  3. Create a loop that will trigger on each child node, using the children() function
  4. Output the element name and data for each child node

A sample code to extract the data using simple XML is as follows,

<?php
 $xml = simplexml_load_file("test.xml");

 echo $xml->getName() . "<br />";

 foreach($xml->children() as $child)
 {
  echo $child->getName() . ": " . $child . "<br />";
 }
?> 

This should help you in retrieving the data.Thanks.

Upvotes: 0

Related Questions