1,105,375 Community Members

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

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

How about:

$nodes = $dom->getElementsByTagName('Equipment');
foreach ($nodes as $node) {
    // other code here based on $node
}
Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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?

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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"); 

}
Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
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.
Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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;
            }
        }
    }
}
Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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;
        }

    }
}
Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

but still not working

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

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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.

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

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

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
1
 

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

Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
1
 

Groundhog Day :)

Member Avatar
iamthwee
Posting Sage
7,066 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 595 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 
Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
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;
        }
    }
}
Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
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: