FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » php xml DOM/xpath how to reference child nodes by name within foreach loop?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
php xml DOM/xpath how to reference child nodes by name within foreach loop? [message #171605] Tue, 11 January 2011 22:43 Go to previous message
inetquestion is currently offline  inetquestion
Messages: 2
Registered: January 2011
Karma:
Junior Member
I am having trouble extracting information from this xml document in
php. The methods I’ve seen for printing the elements under each
<server> do it blindly, looking only to see if it’s a child of
<server>. What I would like to do is reference each element by name,
but am not sure how…
Any suggestions?

<?php
$xmlDOM = new DOMDocument();
$xmlDOM->load("servers.xml");
$SERVERS = $xmlDOM->getElementsByTagName("server");
foreach ($SERVERS AS $svr)
{
##### Print elements of <server> HERE #####
##### #####
##### How do you reference a specific node name at this #####
##### point which is a child to the current node? #####
print $svr->nodeName;
print $svr->getAttribute('offset');
print $svr->getAttribute('ntpd');
}
?>


<?xml version="1.0" encoding="ISO-8859-1"?>
<servers>
<server>
<hostname>hostname01</hostname>
<offset>0</offset>
<ntpd>running</ntpd>
</server>

<server>
<hostname>hostname02</hostname>
<offset>0</offset>
<ntpd>ERROR: timeout</ntpd>
</server>
</servers>
</xml>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Intercepting a HTTP request
Next Topic: input a section of a large file
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 13:20:07 GMT 2024

Total time taken to generate the page: 0.04862 seconds