phpxmlxpathsimplexml

Extract xml element from simplexml_load_string using attribute name


I want to extract sm_field_base_url from the below mentioned XML bsed on the attribute name (sm_field_base_url) or xpath. How can I achieve that. I am using simplexml_load_string() to parse the xml

EDIT : How can I extract content from:

<date name="ds_changed">2014-01-17T05:25:56Z</date>

or
<long name="is_uid">1</long>

or Titan Raga Women Pearly White Dial Watch Coupons @ Myntra or node/95260 or p/titan-raga-women-pearly-white-dial-watch-myntracom-watches

Below is the XML:

 - <response>
         <result name="response" numFound="223" start="0" maxScore="1.4879669">
           <doc>
             <float name="score">1.4879669</float>
             <bool 

       name="bs_promote">false</bool>
             <str name="bundle_name">Product and Coupon</str>
             <str name="content"> Brand:  Titan Create Combination:  No Product Popularity:  0 Best 

       Coupon Status:  0 Retailer:  Myntra.com Category:  Watches Best Coupon Status Display:  Without Coupons Savings:  INR 0 Net Price: 
   INR 2 700 List Price:  INR 2 700 

       Product Name:  Titan Raga Women Pearly White Dial Watch MRPProductPrice:  INR 0 Best Coupon CouponCode:  Base Url: 
   http://www.myntra.com/Watches/Titan/Titan-Raga-

       Women-Pearly-White-Dial-Watch/135793/buy LastCheckedTime:  Friday, January 17, 2014- 10:52 Best Coupon:  </str>
             <arr name="dm_field_lastcheckedtime">
               <date>2014-01-

       17T05:22:42Z</date>
             </arr>
             <date name="ds_changed">2014-01-17T05:25:56Z</date>
             <long name="is_uid">1</long>
             <str name="label">Titan Raga Women Pearly White Dial Watch 

       Coupons @ Myntra</str>
             <str name="path">node/95260</str>
             <str name="path_alias">p/titan-raga-women-pearly-white-dial-watch-myntracom-watches</str>
             <str 

       name="site">http://couponvoodoo.com/</str>
             <arr name="**sm_field_base_url**">
               <str>http://www.myntra.com/Watches/Titan/Titan-Raga-Women-Pearly-White-Dial-

       watch/135793/buy</str>
             </arr>
             <arr name="sm_vid_Retailer">
               <str>Myntra.com</str>
             </arr>
             <arr name="spell">
               <str>Titan Raga Women Pearly White Dial Watch Coupons @ 

       Myntra</str>
               <str> Brand:  Titan Create Combination:  No Product Popularity:  0 Best Coupon Status:  0 Retailer:  Myntra.com Category:
   Watches Best Coupon Status 

       Display:  Without Coupons Savings:  INR 0 Net Price:  INR 2 700 List Price:  INR 2 700 Product Name:  Titan Raga Women Pearly White
   Dial Watch MRPProductPrice:  INR 0 

       Best Coupon CouponCode:  Base Url:  http://www.myntra.com/Watches/Titan/Titan-Raga-Women-Pearly-White-Dial-Watch/135793/buy
   LastCheckedTime:  Friday, January 17, 2014- 

       10:52 Best Coupon:  </str>
               <str>Titan</str>
               <str>Myntra.com</str>
               <str>Watches Women</str>
             </arr>
             <str name="ss_language">und</str>
             <str name="ss_name">admin</str>
             <str 

       name="ss_name_formatted">admin</str>
             <str name="teaser"> Brand:  Titan Create Combination:  No Product Popularity:  0 Best Coupon Status:  0 Retailer:  Myntra.com 

       Category:  Watches Best Coupon Status Display:  Without Coupons Savings:  INR 0 Net Price:  INR 2 700 List Price:  INR 2 700 Product
   Name:  Titan Raga Women Pearly 

       White Dial Watch</str>
             <arr name="tid">
               <long>7979</long>
               <long>8</long>
               <long>62</long>
               <long>46</long>
             </arr>
             <date name="timestamp">2014-01-29T05:52:45.478Z</date>
             <arr 

       name="tm_vid_2_names">
               <str>Myntra.com</str>
             </arr>
             <arr name="tm_vid_3_names">
               <str>Watches Women</str>
             </arr>
             <arr name="tm_vid_4_names">
               <str>Titan</str>
             </arr>
             <str 

       name="tos_name">admin</str>
             <str name="tos_name_formatted">admin</str>
             <str name="url">http://couponvoodoo.com/p/titan-raga-women-pearly-white-dial-watch-myntracom-

       watches</str>
           </doc>
         </result>
       </response>

Solution

  • The following xpath-expression

    $results = $xml->xpath("//arr[@name = 'sm_field_base_url']/str");
    

    will select all <arr> nodes with that name-attribute and return their child <str>.
    $results will be an array of objects. To save the results as string, do:

    $results = array_map('strval', $results);