1.11M Members

How to read ALL data with XPath/XML/PHP from XML

 
0
 

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

 
0
 

How about:

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

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

 
0
 

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

 
0
 
$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"); 

}
 
0
 

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

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

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

 
0
 

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

    }
}
 
0
 

but still not working

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

 
0
 

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.

 
0
 

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

 
0
 

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

 
1
 

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

 
1
 

Groundhog Day :)

 
0
 

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

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.

 
0
 

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

 
0
 

Feel free to take over...

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: