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

问题描述 投票:0回答:1

我想从下面提到的基于属性名称(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/titan-raga-women-pearly-white-dial-watch-myntracom-watches

以下是 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>
php xml xpath simplexml
1个回答
1
投票

以下 xpath 表达式

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

将选择具有该名称属性的所有

<arr>
节点并返回其子节点
<str>

$results
将是
objects
的数组。要将结果保存为
string
,请执行以下操作:

$results = array_map('strval', $results);
© www.soinside.com 2019 - 2024. All rights reserved.