PriteshP23 Posted July 17, 2013 Share Posted July 17, 2013 Hello All, I would like to get all Equipment values. I have used DOM Document. It is reading ONLY LAST Element.Equipment Id: 28. I need all three. Thanks in advanced. XML File: <?xml version="1.0" encoding="UTF-8"?> -<Physical> <Catalog> </Catalog> -<Installed> -<Equipment> <Id>26</Id> <Ref>Tew12</Ref> -<Characteristic> <CharacteristicName>Height</CharacteristicName> <CharacteristicValue>160</CharacteristicValue> </Characteristic> -<Characteristic> <CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>30</CharacteristicValue> </Equipment> -<Equipment> <Id>27</Id> <Ref>Tew13</Ref> -<Characteristic> <CharacteristicName>Height</CharacteristicName> <CharacteristicValue>165</CharacteristicValue> </Characteristic> -<Characteristic> <CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>50</CharacteristicValue> </Equipment> -<Equipment> <Id>28</Id> <Ref>Tew14</Ref> -<Characteristic> <CharacteristicName>Height</CharacteristicName> <CharacteristicValue>190</CharacteristicValue> </Characteristic> -<Characteristic> <CharacteristicName>Tilt</CharacteristicName> <CharacteristicValue>50</CharacteristicValue> </Equipment> </Installed> </Physical> Expected Result: Id 26 Tilt 30 Id 27 Tilt 50 Id 28 Tilt 50 For the moment, i am trying to get all Ids values. Still it is not giving ALL Ids. It should give Ids: 26 27 28 $Equipments = $dom->getElementsByTagName('Equipment'); foreach( $Equipments as $Equipments ) { $Ids = $Equipments->getElementsByTagName( 'Id' ); $Id = $Ids->item(0)->nodeValue; echo $Id; } Quote Link to comment https://forums.phpfreaks.com/topic/280256-how-to-read-all-data-with-xpathxmlphp-from-xml/ Share on other sites More sharing options...
Barand Posted July 17, 2013 Share Posted July 17, 2013 That xml is missing several </characteristic> tags Quote Link to comment https://forums.phpfreaks.com/topic/280256-how-to-read-all-data-with-xpathxmlphp-from-xml/#findComment-1441149 Share on other sites More sharing options...
Barand Posted July 17, 2013 Share Posted July 17, 2013 try SimpleXML $xml = simplexml_load_file('equip.xml'); foreach ($xml->xpath('//Equipment') as $eq) { echo "ID: {$eq->Id}<br>"; foreach ($eq->Characteristic as $c) { if ($c->CharacteristicName == 'Tilt') { echo "Tilt: {$c->CharacteristicValue}<br>"; } } echo '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/280256-how-to-read-all-data-with-xpathxmlphp-from-xml/#findComment-1441152 Share on other sites More sharing options...
PriteshP23 Posted July 18, 2013 Author Share Posted July 18, 2013 @Barand: First of all thanks for your time. It is giving all IDs. But it not working for Tilt values. Quote Link to comment https://forums.phpfreaks.com/topic/280256-how-to-read-all-data-with-xpathxmlphp-from-xml/#findComment-1441206 Share on other sites More sharing options...
Solution PriteshP23 Posted July 18, 2013 Author Solution Share Posted July 18, 2013 foreach ($xml->xpath('//Equipment') as $eq) { $Id = $eq->Id; foreach ($eq->Characteristic as $c) { if ($c->CharacteristicName == 'Tilt') { $Tilt = $c->CharacteristicValue; } } THANKS A LOT. Quote Link to comment https://forums.phpfreaks.com/topic/280256-how-to-read-all-data-with-xpathxmlphp-from-xml/#findComment-1441221 Share on other sites More sharing options...
Barand Posted July 18, 2013 Share Posted July 18, 2013 @Barand: First of all thanks for your time. It is giving all IDs. But it not working for Tilt values. Odd. After fixing the XML, my output was ID: 26 Tilt: 30 ID: 27 Tilt: 50 ID: 28 Tilt: 50 Quote Link to comment https://forums.phpfreaks.com/topic/280256-how-to-read-all-data-with-xpathxmlphp-from-xml/#findComment-1441230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.