Start New Discussion within our Web Development Community

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

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

    }
}

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

Comments
patience is a virtue - you're so cool :)

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.

This question has already been answered. Start a new discussion instead.