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

CODE:
http://www.daniweb.com/web-development/php/threads/458094/how-to-get-nodevalue-with-xpathxmlphp

How about:

$nodes = $dom->getElementsByTagName('Equipment');
foreach ($nodes as $node) {
    // other code here based on $node
}

@pritaeas:

It is not giving ALL ids. Thanks for help. Please look at code. I need to read ALL equipments from the XML file.

That means all Ids & Tilts. I hope i am clear to you.

I believe there should be two loops. Any other idea?

You just have to merge this loop with the one you already have (with an adjustment). Show what you have tried.

$nodes = $dom->getElementsByTagName('Equipment');
foreach ($nodes as $node) 
{
    $node = $xml->xpath("/Physical/Installed/Equipment/Id");
    foreach ($node as $Id)
    {
       print($node->nodeValue);
    }
    $childnodes = $dom->getElementsByTagName('Characteristic');
    foreach ($childnodes as $childnode) 
    {


        foreach ($childnode as $TiltValue)
        {  
            if ($TiltValue->CharacteristicName == 'Tilt')
            {        
                $TiltValue->CharacteristicValue;
                $Tilt=$TiltValue->CharacteristicValue;
            }
        }
    }
    fputs($OutputData, $Id.";".$Tilt."\n"); 

}

Just pasting it together won't work. Should be something like this (untested):

foreach ($nodes as $node) 
{
    $ids = $node->xpath("Id");
    foreach ($ids as $id)
    {
       print($id->nodeValue);
    }

    $childnodes = $node->getElementsByTagName('Characteristic');
    foreach ($childnodes as $childnode) 
    {
        // etc.

It is not working for IDs and Tilt Value.

$nodes = $dom->getElementsByTagName('Equipment');
foreach ($nodes as $node) 
{    
    $ids = $node->xpath("Id");
    foreach ($ids as $Id)
    {       
        print($Id->nodeValue);
    }

    $childnodes = $node->getElementsByTagName("Characteristic");
    foreach ($childnodes as $childnode)
    {        

        $childnode = $node->xpath("/Physical/Installed/Equipment/Characteristic/");
        foreach ($childnode as $TiltValue)
        {  
            if ($TiltValue->CharacteristicName == 'Tilt')
            {        
                $TiltValue->CharacteristicValue;
                $Tilt=$TiltValue->CharacteristicValue;
            }
        }
    }
}

Line 14 and 15 shouldn't be there. Rename $childnode on line 11.

I did it; but still not working. :(

$nodes = $dom->getElementsByTagName('Equipment');
foreach ($nodes as $node) 
{    
    $ids = $node->xpath("Id");
    foreach ($ids as $Id)
    {       
        print($Id->nodeValue);
    }

    $childnodes = $node->getElementsByTagName("Characteristic");
    foreach ($childnodes as $TiltValue)
    {        
        if ($TiltValue->CharacteristicName == 'Tilt')
        {        
            $TiltValue->CharacteristicValue;
            $Tilt=$TiltValue->CharacteristicValue;
        }

    }
}

but still not working

Can you be more specific? Lines 15 and 16 are not outputting anything.

Can you be more specific?

Actually, i need to get values of IDs and Tilt. In text file, i am saving this values by fputs() command in php.
Expected Result:

Id 26
Tilt 30

Id 27
Tilt 50

Id 28
Tilt 50

For this reason, i wrote line 14 & 15. This may be not good solution to get the expected result. Please let me know if you need more info.

Can you be more specific about what's not working? Do you get errors? What have you tried? Repeating the question isn't useful.

It is not printing the ID values. I ALREADY gave my code. There is no error mesg.

I already told you that you are not printing anything on line 15 and 16...

commented: patience is a virtue - you're so cool :) +14
Member Avatar for diafol

Groundhog Day :)

commented: Sigh... Epic movie, was on yesterday, still loving it. +14

I did like this. Can anyone correct it?

There is no error mesg but it is not working.

$Equipments = $dom->getElementsByTagName('Equipment');
foreach($Equipments as $Equipment)
{
    $Idquery = $Equipment->getAttribute('Id');
    foreach ($Idquery as $Id)
    {
       print($Idquery->nodeValue);
    }
    $Characteristics = $xml->xpath("/Physical/Installed/Equipment/Characteristic");
    foreach ($Characteristics as $Characteristic)
    { 
    if ($TiltValue->CharacteristicName == 'Tilt')
        {        
            $TiltValue->CharacteristicValue;
            $Tilt=$TiltValue->CharacteristicValue;
        }
    }
}

This is the last time, line 14 and 15 do not have an echo or print statement, so nothing is outputted. On line 9 you still haven't changed the xpath to what I showed you.

Member Avatar for diafol

Heh heh heh. This is like a sketch from a comedy show. Soon to become slapstick...

Feel free to take over...

This is the last time, line 14 and 15 do not have an echo or print statement, so nothing is outputted. On line 9 you still haven't changed the xpath to what I showed you.

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;
} 

Finally, I did it..!! :)

foreach ($xml->xpath('//Equipment') as $eq) 
{
    $Id = $eq->Id;
    foreach ($eq->Characteristic as $c) 
    {
        if ($c->CharacteristicName == 'Tilt') 
        {
            $Tilt = $c->CharacteristicValue;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.