Read ALL data with XPath/XML/PHP from XML

General discussions related to php

Moderators: macek, egami, gesf

Pritesh23
New php-forum User
New php-forum User
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Read ALL data with XPath/XML/PHP from XML

Postby Pritesh23 » Wed Jul 17, 2013 12:51 pm

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:

Code: Select all

<?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

Code: Select all

$Equipments = $dom->getElementsByTagName('Equipment');
foreach( $Equipments as $Equipments )
{
    $Ids = $Equipments->getElementsByTagName( 'Id' );
    $Id = $Ids->item(0)->nodeValue;
    echo $Id;
}
 

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Read ALL data with XPath/XML/PHP from XML

Postby johnj » Wed Jul 17, 2013 8:14 pm

try this code:

Code: Select all

$Equipments = $dom->getElementsByTagName('Equipment');
foreach( $Equipments as $Equipment )
{
    $Ids = $Equipment->getElementsByTagName( 'Id' );
    $Id = $Ids->item(0)->nodeValue;
    echo $Id;
}

Pritesh23
New php-forum User
New php-forum User
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: Read ALL data with XPath/XML/PHP from XML

Postby Pritesh23 » Thu Jul 18, 2013 12:01 am

@johnj:

try this code:


First of all thanks for your time.

I tried your code. It is not working. :help:

Any other idea??

Pritesh23
New php-forum User
New php-forum User
Posts: 25
Joined: Wed Jul 17, 2013 12:42 pm

Re: Read ALL data with XPath/XML/PHP from XML

Postby Pritesh23 » Thu Jul 18, 2013 2:58 am

THANKS A LOT. :D

foreach ($xml->xpath('//Equipment') as $eq)
{
$Id = $eq->Id;
$Reference = $eq->Reference;
$Site = $eq->Site;
$Cellule = $eq->Cellule;

foreach ($eq->Characteristic as $c)
{
if ($c->CharacteristicName == 'Usage')
{
$Usage = $c->CharacteristicValue;
}


SOLVED


Return to “PHP General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest