2017-09-22 144 views
0

我試圖用PHP讀取XML文件,但我無法獲取數據並找不到原因。PHP:從XML集合中獲取結果

XML結構:

<?xml version="1.0" encoding="UTF-8"?> 
<FavoriteListResponse> 
    <metadata> 
     <offset>0</offset> 
     <limit>20</limit> 
     <total>1</total> 
    </metadata> 
    <favorites> 
     <favorites objectType="Product"> 
     <id>5831fd52f9f0c0a80c88dafa</id> 
     <productId>511290702</productId> 
     <imageId>39d5e666255e351aa42fca3a9b939d04</imageId> 
     <product> 
      <id>511290702</id> 
      <name>Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper</name> 
      <brandedName>Mackage Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper</brandedName> 
      <unbrandedName>Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper</unbrandedName> 
      <currency>USD</currency> 
      <price>620.0</price> 
      <priceLabel>Sold Out</priceLabel> 
      <inStock>false</inStock> 
      <stock> 
       <stock> 
        <size> 
        <name>M</name> 
        </size> 
       </stock> 
       <stock> 
        <size> 
        <name>XXS</name> 
        </size> 
       </stock> 
      </stock> 
      <retailer> 
       <id>596</id> 
       <name>Mackage</name> 
       <score>242</score> 
      </retailer> 
      <brand> 
       <id>1590</id> 
       <name>Mackage</name> 
      </brand> 
      <locale>en_US</locale> 
      <description>Misha by Mackage is a washed leather jacket for women crafted from genuine lambskin. Fitted silhouette cropped above the hips. Asymmetrical full-length zip closure. Dual zippered front pockets. Nickel hardware. Available in black and sand. Fits true to size. Fitted silhouette. Washed genuine lambskin leather jacket. Cropped above the hips. Padded shoulder yoke. Hem yoke with stitching at the back bottom. Asymmetrical full-length two-way zip closure. Nickel hardware. Coat length from shoulder to hem : 17inches/43.2cm Model is 5'11/180 cm and wearing a size XS. Shell: 100% Lambskin. Lining (Woven): 100% Polyester.</description> 
      <clickUrl>https://api.shopstyle.com/action/apiVisitRetailer?id=511290702&amp;pid=uid5044-35323862-95</clickUrl> 
      <image> 
       <sizes> 
        <Small> 
        <sizeName>Small</sizeName> 
        <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url> 
        <width>32</width> 
        <height>40</height> 
        <actualWidth>11</actualWidth> 
        <actualHeight>40</actualHeight> 
        </Small> 
        <XLarge> 
        <sizeName>XLarge</sizeName> 
        <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_xlarge.jpg</url> 
        <width>328</width> 
        <height>410</height> 
        <actualWidth>111</actualWidth> 
        <actualHeight>410</actualHeight> 
        </XLarge> 
        <Medium> 
        <sizeName>Medium</sizeName> 
        <url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
        <width>112</width> 
        <height>140</height> 
        <actualWidth>38</actualWidth> 
        <actualHeight>140</actualHeight> 
        </Medium> 
        <Large> 
        <sizeName>Large</sizeName> 
        <url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
        <width>164</width> 
        <height>205</height> 
        <actualWidth>55</actualWidth> 
        <actualHeight>205</actualHeight> 
        </Large> 
        <IPhoneSmall> 
        <sizeName>IPhoneSmall</sizeName> 
        <url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url> 
        <width>100</width> 
        <height>125</height> 
        <actualWidth>34</actualWidth> 
        <actualHeight>125</actualHeight> 
        </IPhoneSmall> 
        <Best> 
        <sizeName>Best</sizeName> 
        <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url> 
        <width>720</width> 
        <height>900</height> 
        <actualWidth>243</actualWidth> 
        <actualHeight>900</actualHeight> 
        </Best> 
        <Original> 
        <sizeName>Original</sizeName> 
        <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url> 
        <actualWidth>243</actualWidth> 
        <actualHeight>900</actualHeight> 
        </Original> 
        <IPhone> 
        <sizeName>IPhone</sizeName> 
        <url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04.jpg</url> 
        <width>288</width> 
        <height>360</height> 
        <actualWidth>97</actualWidth> 
        <actualHeight>360</actualHeight> 
        </IPhone> 
       </sizes> 
       <id>39d5e666255e351aa42fca3a9b939d04</id> 
      </image> 
      <alternateImages> 
       <alternateImages> 
        <sizes> 
        <Small> 
         <sizeName>Small</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url> 
         <width>32</width> 
         <height>40</height> 
         <actualWidth>11</actualWidth> 
         <actualHeight>40</actualHeight> 
        </Small> 
        <XLarge> 
         <sizeName>XLarge</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_xlarge.jpg</url> 
         <width>328</width> 
         <height>410</height> 
         <actualWidth>111</actualWidth> 
         <actualHeight>410</actualHeight> 
        </XLarge> 
        <Medium> 
         <sizeName>Medium</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>112</width> 
         <height>140</height> 
         <actualWidth>38</actualWidth> 
         <actualHeight>140</actualHeight> 
        </Medium> 
        <Large> 
         <sizeName>Large</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/39/d5/39d5e666255e351aa42fca3a9b939d04/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>164</width> 
         <height>205</height> 
         <actualWidth>55</actualWidth> 
         <actualHeight>205</actualHeight> 
        </Large> 
        <IPhoneSmall> 
         <sizeName>IPhoneSmall</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04_small.jpg</url> 
         <width>100</width> 
         <height>125</height> 
         <actualWidth>34</actualWidth> 
         <actualHeight>125</actualHeight> 
        </IPhoneSmall> 
        <Best> 
         <sizeName>Best</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url> 
         <width>720</width> 
         <height>900</height> 
         <actualWidth>243</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Best> 
        <Original> 
         <sizeName>Original</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/39/d5/39d5e666255e351aa42fca3a9b939d04_best.jpg</url> 
         <actualWidth>243</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Original> 
        <IPhone> 
         <sizeName>IPhone</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/39/d5/39d5e666255e351aa42fca3a9b939d04.jpg</url> 
         <width>288</width> 
         <height>360</height> 
         <actualWidth>97</actualWidth> 
         <actualHeight>360</actualHeight> 
        </IPhone> 
        </sizes> 
        <id>39d5e666255e351aa42fca3a9b939d04</id> 
       </alternateImages> 
       <alternateImages> 
        <sizes> 
        <Small> 
         <sizeName>Small</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_small.jpg</url> 
         <width>32</width> 
         <height>40</height> 
         <actualWidth>12</actualWidth> 
         <actualHeight>40</actualHeight> 
        </Small> 
        <XLarge> 
         <sizeName>XLarge</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_xlarge.jpg</url> 
         <width>328</width> 
         <height>410</height> 
         <actualWidth>124</actualWidth> 
         <actualHeight>410</actualHeight> 
        </XLarge> 
        <Medium> 
         <sizeName>Medium</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/56/c4/56c4c2a990fb868e7a27a57381330244_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>112</width> 
         <height>140</height> 
         <actualWidth>42</actualWidth> 
         <actualHeight>140</actualHeight> 
        </Medium> 
        <Large> 
         <sizeName>Large</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/56/c4/56c4c2a990fb868e7a27a57381330244/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>164</width> 
         <height>205</height> 
         <actualWidth>62</actualWidth> 
         <actualHeight>205</actualHeight> 
        </Large> 
        <IPhoneSmall> 
         <sizeName>IPhoneSmall</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/56/c4/56c4c2a990fb868e7a27a57381330244_small.jpg</url> 
         <width>100</width> 
         <height>125</height> 
         <actualWidth>38</actualWidth> 
         <actualHeight>125</actualHeight> 
        </IPhoneSmall> 
        <Best> 
         <sizeName>Best</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_best.jpg</url> 
         <width>720</width> 
         <height>900</height> 
         <actualWidth>273</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Best> 
        <Original> 
         <sizeName>Original</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/56/c4/56c4c2a990fb868e7a27a57381330244_best.jpg</url> 
         <actualWidth>273</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Original> 
        <IPhone> 
         <sizeName>IPhone</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/56/c4/56c4c2a990fb868e7a27a57381330244.jpg</url> 
         <width>288</width> 
         <height>360</height> 
         <actualWidth>109</actualWidth> 
         <actualHeight>360</actualHeight> 
        </IPhone> 
        </sizes> 
        <id>56c4c2a990fb868e7a27a57381330244</id> 
       </alternateImages> 
       <alternateImages> 
        <sizes> 
        <Small> 
         <sizeName>Small</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_small.jpg</url> 
         <width>32</width> 
         <height>40</height> 
         <actualWidth>10</actualWidth> 
         <actualHeight>40</actualHeight> 
        </Small> 
        <XLarge> 
         <sizeName>XLarge</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_xlarge.jpg</url> 
         <width>328</width> 
         <height>410</height> 
         <actualWidth>108</actualWidth> 
         <actualHeight>410</actualHeight> 
        </XLarge> 
        <Medium> 
         <sizeName>Medium</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>112</width> 
         <height>140</height> 
         <actualWidth>37</actualWidth> 
         <actualHeight>140</actualHeight> 
        </Medium> 
        <Large> 
         <sizeName>Large</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>164</width> 
         <height>205</height> 
         <actualWidth>54</actualWidth> 
         <actualHeight>205</actualHeight> 
        </Large> 
        <IPhoneSmall> 
         <sizeName>IPhoneSmall</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_small.jpg</url> 
         <width>100</width> 
         <height>125</height> 
         <actualWidth>33</actualWidth> 
         <actualHeight>125</actualHeight> 
        </IPhoneSmall> 
        <Best> 
         <sizeName>Best</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_best.jpg</url> 
         <width>720</width> 
         <height>900</height> 
         <actualWidth>236</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Best> 
        <Original> 
         <sizeName>Original</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c_best.jpg</url> 
         <actualWidth>236</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Original> 
        <IPhone> 
         <sizeName>IPhone</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/a9/b2/a9b2551df3fe88d30f596ab83a1e764c.jpg</url> 
         <width>288</width> 
         <height>360</height> 
         <actualWidth>94</actualWidth> 
         <actualHeight>360</actualHeight> 
        </IPhone> 
        </sizes> 
        <id>a9b2551df3fe88d30f596ab83a1e764c</id> 
       </alternateImages> 
       <alternateImages> 
        <sizes> 
        <Small> 
         <sizeName>Small</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_small.jpg</url> 
         <width>32</width> 
         <height>40</height> 
         <actualWidth>11</actualWidth> 
         <actualHeight>40</actualHeight> 
        </Small> 
        <XLarge> 
         <sizeName>XLarge</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_xlarge.jpg</url> 
         <width>328</width> 
         <height>410</height> 
         <actualWidth>114</actualWidth> 
         <actualHeight>410</actualHeight> 
        </XLarge> 
        <Medium> 
         <sizeName>Medium</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>112</width> 
         <height>140</height> 
         <actualWidth>39</actualWidth> 
         <actualHeight>140</actualHeight> 
        </Medium> 
        <Large> 
         <sizeName>Large</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/b8/2a/b82a3ff82915a288cedb59386eb74c4c/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>164</width> 
         <height>205</height> 
         <actualWidth>57</actualWidth> 
         <actualHeight>205</actualHeight> 
        </Large> 
        <IPhoneSmall> 
         <sizeName>IPhoneSmall</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_small.jpg</url> 
         <width>100</width> 
         <height>125</height> 
         <actualWidth>35</actualWidth> 
         <actualHeight>125</actualHeight> 
        </IPhoneSmall> 
        <Best> 
         <sizeName>Best</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_best.jpg</url> 
         <width>720</width> 
         <height>900</height> 
         <actualWidth>250</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Best> 
        <Original> 
         <sizeName>Original</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/b8/2a/b82a3ff82915a288cedb59386eb74c4c_best.jpg</url> 
         <actualWidth>250</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Original> 
        <IPhone> 
         <sizeName>IPhone</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/b8/2a/b82a3ff82915a288cedb59386eb74c4c.jpg</url> 
         <width>288</width> 
         <height>360</height> 
         <actualWidth>100</actualWidth> 
         <actualHeight>360</actualHeight> 
        </IPhone> 
        </sizes> 
        <id>b82a3ff82915a288cedb59386eb74c4c</id> 
       </alternateImages> 
       <alternateImages> 
        <sizes> 
        <Small> 
         <sizeName>Small</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_small.jpg</url> 
         <width>32</width> 
         <height>40</height> 
         <actualWidth>32</actualWidth> 
         <actualHeight>40</actualHeight> 
        </Small> 
        <XLarge> 
         <sizeName>XLarge</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_xlarge.jpg</url> 
         <width>328</width> 
         <height>410</height> 
         <actualWidth>328</actualWidth> 
         <actualHeight>410</actualHeight> 
        </XLarge> 
        <Medium> 
         <sizeName>Medium</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/e4/9d/e49db198c964ac5c339b2924687c633c_medium/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>112</width> 
         <height>140</height> 
         <actualWidth>112</actualWidth> 
         <actualHeight>140</actualHeight> 
        </Medium> 
        <Large> 
         <sizeName>Large</sizeName> 
         <url>https://img.shopstyle-cdn.com/sim/e4/9d/e49db198c964ac5c339b2924687c633c/mackage-misha-black-cropped-leather-jacket-with-asymmetrical-front-zipper.jpg</url> 
         <width>164</width> 
         <height>205</height> 
         <actualWidth>164</actualWidth> 
         <actualHeight>205</actualHeight> 
        </Large> 
        <IPhoneSmall> 
         <sizeName>IPhoneSmall</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/e4/9d/e49db198c964ac5c339b2924687c633c_small.jpg</url> 
         <width>100</width> 
         <height>125</height> 
         <actualWidth>100</actualWidth> 
         <actualHeight>125</actualHeight> 
        </IPhoneSmall> 
        <Best> 
         <sizeName>Best</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_best.jpg</url> 
         <width>720</width> 
         <height>900</height> 
         <actualWidth>720</actualWidth> 
         <actualHeight>900</actualHeight> 
        </Best> 
        <Original> 
         <sizeName>Original</sizeName> 
         <url>https://img.shopstyle-cdn.com/pim/e4/9d/e49db198c964ac5c339b2924687c633c_best.jpg</url> 
        </Original> 
        <IPhone> 
         <sizeName>IPhone</sizeName> 
         <url>https://img.shopstyle-cdn.com/mim/e4/9d/e49db198c964ac5c339b2924687c633c.jpg</url> 
         <width>288</width> 
         <height>360</height> 
         <actualWidth>288</actualWidth> 
         <actualHeight>360</actualHeight> 
        </IPhone> 
        </sizes> 
        <id>e49db198c964ac5c339b2924687c633c</id> 
       </alternateImages> 
      </alternateImages> 
      <extractDate>2016-02-16</extractDate> 
      <lastModified>2017-02-05</lastModified> 
      <seeMoreLabel>Mackage Leather Jackets</seeMoreLabel> 
      <preOwned>false</preOwned> 
      <rental>false</rental> 
      <colors /> 
      <sizes> 
       <size> 
        <name>XXS</name> 
        <canonicalSize> 
        <id>79</id> 
        <name>XXS (0)</name> 
        </canonicalSize> 
       </size> 
       <size> 
        <name>M</name> 
        <canonicalSize> 
        <id>85</id> 
        <name>M (8-10)</name> 
        </canonicalSize> 
       </size> 
      </sizes> 
      <categories> 
       <category> 
        <id>leather-jackets</id> 
        <name>Leather Jackets</name> 
        <shortName>Leather</shortName> 
        <fullName>Women's Leather Jackets</fullName> 
        <localizedId>leather-jackets</localizedId> 
       </category> 
      </categories> 
      <badges /> 
     </product> 
     <date> 
      <date>2016-11-20</date> 
      <timestamp>1479671122</timestamp> 
      <friendly>Nov 20, 2016</friendly> 
     </date> 
     <objectId>511290702</objectId> 
     </favorites> 
    </favorites> 
</FavoriteListResponse> 

我無法弄清楚。希望有人願意幫助:)

+0

你可以用'simplexml_load_string'如果** ** XML是一個字符串變量,如果it'a文件使用'simplexml_load_file'可以關於如何使用這兩個手冊參考:http://php.net/manual/fr/function.simplexml-load-file.php – teeyo

回答

0

SimpleXML的是任何簡單的方法去爲這個數據。此代碼假定數據在文件中,但只需將simplexml_load_file更改爲simplexml_load_string並將XML傳遞到那裏。

<?php 
error_reporting (E_ALL); 
ini_set ('display_errors', 1); 


$xml = simplexml_load_file('t1.xml'); 
foreach($xml->favorites->favorites->product as $product){ 
    echo "Product name=".(string)$product->name.PHP_EOL; 
    echo "Retailer=".(string)$product->retailer->name.PHP_EOL; 
    foreach ($product->sizes->size as $size) { 
     echo "Size:".(string)$size->name.PHP_EOL; 
    } 

    echo "Small image=".(string)$product->image->sizes->Small->url; 
} 

這隻顯示部分數據,但我希望它給出瞭如何使用XML結構的想法。使用$xml->favorites可爲您提供$ xml值中的收藏夾元素。因此$xml->favorites->favorites->product將通過每個產品,然後$product->name給你的名稱元素。從每種產品中,此代碼使用$product->sizes->size來檢查每種尺寸,但對於任何數據項都可以做到這一點。

此輸出(與你的示例XML):

Product name=Misha Black Cropped Leather Jacket With Asymmetrical Front Zipper 
Retailer=Mackage 
Size:XXS 
Size:M 
+0

這是工作,但只顯示一個產品。 –

+0

可以請你幫我解決這個問題嗎?我怎樣才能得到我真正感到沮喪的形象。首先我這樣做,所以請幫助我@Nigel Ren –

+0

示例中只有1種產品您附加的XML。我還添加了一行提取圖片網址的其中一個。應該能夠改變這個不同的圖像。 –

0

我通常使用這個對我的工作的目的,您就可以使用這是一個嘗試,

/** 
* Class xml2array 
*/ 
class xml2array { 

    var $arrOutput = array(); 
    var $resParser; 
    var $strXmlData; 

    function parse($strInputXML) { 

     $this->resParser = xml_parser_create(); 
     xml_set_object($this->resParser,$this); 
     xml_set_element_handler($this->resParser, "tagOpen", "tagClosed"); 

     xml_set_character_data_handler($this->resParser, "tagData"); 

     $this->strXmlData = xml_parse($this->resParser,$strInputXML); 
     if(!$this->strXmlData) { 
      die(sprintf("XML error: %s at line %d", 
       xml_error_string(xml_get_error_code($this->resParser)), 
       xml_get_current_line_number($this->resParser))); 
     } 

     xml_parser_free($this->resParser); 

     return $this->arrOutput; 
    } 
    function tagOpen($parser, $name, $attrs) { 
     $tag=array("name"=>$name,"attrs"=>$attrs); 
     array_push($this->arrOutput,$tag); 
    } 

    function tagData($parser, $tagData) { 
     if(trim($tagData)) { 
      if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) { 
       $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData; 
      } 
      else { 
       $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData; 
      } 
     } 
    } 

    function tagClosed($parser, $name) { 
     $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1]; 
     array_pop($this->arrOutput); 
    } 
} 


$xml_file = file_get_contents('yourxmlfilename.xml'); 
$objXML = new xml2array(); 
$arrOutput = $objXML->parse($xml_file); 

print '<pre>'; 
print_r($arrOutput); //print it out, or do whatever! 
print '</pre>'; 
0
function actionFindDataByXMLTag($result, $xml_tag, $xml_end_tag, $count = null) { 

    $next_string = ''; 

    $result_array = array(); 
    $stop = 500;//only 500 items values will be selected 

    $i = 0; 
    while($i < $stop) { 

     if($next_string == '') { 

      $from = $result; 
     } else { 
      $from = $next_string; 
     } 

     $position = strpos($from, $xml_tag) + strlen($xml_tag); 
     $start = substr($from, $position); 
     $end = strpos($start, $xml_end_tag); 

     $terr_result = substr($start, 0, $end); 

     if($terr_result == '') { 
      break; 
     } else { 
      array_push($result_array, $terr_result); 
     } 

     $next_string = substr($start, $end + strlen($xml_end_tag)); 

     //var_dump($next_string); 

     $i++; 
    } 

    return $result_array; 
}