Search in This Blog

Parse XML in PHP with Attributes

Problem:
How to Parse XML in PHP with Attributes
Listed below the XML data.
Suppose you have a url which produce below XML data on request and you want to parse it in PHP including it's attribute for ex:  spell id="168839" minCount="1" maxCount="1" etc.

See below XML for full information:
<?xml version="1.0" encoding="utf-8"?>
<wowhead><item id="114811">
  <name>Hexweave Leggings</name>
  <level>640</level>
  <quality id="4">Epic</quality>
  <class id="4">Armor</class>
  <subclass id="1">Cloth Armor</subclass>
  <icon displayId="132245">inv_cloth_draenorcrafted_d_01pants</icon>
  <inventorySlot id="7">Legs</inventorySlot>
  <htmlTooltip>
    <table>
      <tr>
        <td><!--nstart-->
          <b class="q4">Hexweave Leggings</b>
          <!--nend--><!--ndstart--><!--ndend-->
          <span style="color: #ffd100">
            <br />
            Item Level<!--ilvl-->640</span>
          <br />
          <!--bo-->Binds when equipped
          <br />
          Unique-Equipped: Warlords Crafted (3)
          <table width="100%">
            <tr>
              <td>Legs</td>
              <th>Cloth</th>
            </tr>
          </table>
          <span><!--amr-->83 Armor</span>
          <br />
          <!--re-->
          <span class="q2">&lt;Random enchantment&gt;</span>
          <br />
          <span><!--stat5-->+184 Intellect</span>
          <br />
          <span><!--stat7-->+275 Stamina</span>
          <!--ebstats--><!--egstats--><!--e--><!--ps-->
          <br />
          Durability 120 / 120</td>
      </tr>
    </table>
    <table>
      <tr>
        <td>Requires Level<!--rlvl-->91
          <div class="whtt-sellprice">Sell Price:
            <span class="moneygold">41</span>
            <span class="moneysilver">61</span>
            <span class="moneycopper">90</span>
          </div>
        </td>
      </tr>
    </table>
  </htmlTooltip>
  <json>"appearances":{"6":[132245,""],"7":[132269,""],"8":[132254,""]},"armor":83,"bonustrees":[184],"classs":4,"displayid":132245,"flags2":8192,"id":114811,"level":640,"name":"4Hexweave Leggings","reqlevel":91,"slot":7,"slotbak":7,"source":[1],"sourcemore":[{"c":11,"icon":"inv_cloth_draenorcrafted_d_01pants","n":"Hexweave Leggings","s":197,"t":6,"ti":168839}],"specs":[64,256,63,62,257,265,258,266,267],"subclass":1</json>
  <jsonEquip>"appearances":{"6":[132245,""],"7":[132269,""],"8":[132254,""]},"armor":83,"avgbuyout":53990000,"displayid":132245,"dura":120,"int":184,"reqlevel":91,"sellprice":416190,"slotbak":7,"sta":275</jsonEquip>
  <createdBy>
    <spell id="168839" name="Hexweave Leggings" icon="inv_cloth_draenorcrafted_d_01pants" minCount="1" maxCount="1">
      <reagent id="111556" name="Hexweave Cloth" quality="2" icon="inv_tailoring_hexweavethread" count="100"/>
      <reagent id="110609" name="Raw Beast Hide" quality="1" icon="inv_misc_nativebeastskin" count="4"/>
    </spell>
  </createdBy>
  <link>http://www.wowhead.com/item=114811</link>
  </item></wowhead>


Solution:

Using simplexml_load_file we can parse XMl data.

Example:

<?php
$url='http://www.wowhead.com/item=114811&xml';
$xml = simplexml_load_file($url) or die("feed not loading");
foreach($xml->item  as $items){
    echo $items->name . "<br>";
echo $items->level . "<br>";
echo $items->quality . "<br>";
echo $items->quality['id'] . "<br>";
echo $items->class . "<br>";
echo $items->class['id'] . "<br>";
echo $items->subclass . "<br>";
echo $items->subclass['id'] . "<br>";
echo $items->icon . "<br>";
echo $items->icon['displayId'] . "<br>";
echo $items->inventorySlot . "<br>";
echo $items->inventorySlot['id'] . "<br>";
echo $items->htmlTooltip . "<br>";
echo $items->json . "<br>";
echo $items->jsonEquip . "<br>";
echo $items->createdBy->spell  . " : ";
echo $items->createdBy->spell['id']  ." : ";
echo $items->createdBy->spell['name']  ." : ";
echo $items->createdBy->spell['icon']  . " : ";
echo $items->createdBy->spell['minCount']  ." : ";
echo $items->createdBy->spell['maxCount']  . "<br>";
$created_by_count=0;
foreach($items->createdBy->spell  as $myspel){
if($myspel->count()>=1) {
    foreach($myspel as $child) {
                  $created_by_count++;
      echo $child->getName(). ' : '. $child['id']. ' : ' .$child['name'] . ' : ' . $child['quality']. ' : ' . $child['icon']. ' : ' . $child['count'] . '<br/>';
}
  }
  echo 'Count createdBy='.$created_by_count. "<br>";
echo $items->link . "<br>";
}
}
?>
Previous
Next Post »