使用属性名称从simplexml_load_string中提取xml元素

时间:2014-01-31 10:52:28

标签: xml xpath simplexml

我想从属性名称(sm_field_base_url)或xpath上的下面提到的XML中提取sm_field_base_url。我怎样才能做到这一点。 我正在使用simplexml_load_string()来解析xml

编辑:如何从以下位置提取内容:

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

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

或     Titan Raga女士珍珠白色表盘手表优惠券@ Myntra或 节点/ 95260 要么 P /钛-RAGA-妇女珍珠白拨盘手表myntracom-手表

以下是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>

1 个答案:

答案 0 :(得分:1)

以下xpath-expression

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

将选择具有该name-attribute的所有<arr>个节点并返回其子<str> $results将是objects的数组。要将结果保存为string,请执行:

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