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
Re: php xml DOM/xpath how to reference child nodes by name within foreach loop? [message #171610 is a reply to message #171605] Wed, 12 January 2011 00:48 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On 11/01/11 22:43, inetquestion wrote:

> Any suggestions?

> <?php
> $xmlDOM = new DOMDocument();
> $xmlDOM->load("servers.xml");
> $SERVERS = $xmlDOM->getElementsByTagName("server");

After you have $SERVERS, do this with it:

$items = $SERVERS->length;
$nodenames = array('hostname','offset','ntpd');
while ($items --) {
$server = $SERVERS->item($items);
foreach($nodenames as $nodename) {
$nodes = $server->getElementsByTagName($nodename);
if ($nodes->length == 1) {
$node = $nodes->item(0);
echo $nodename . "=" . $node->textContent . "\n";
}
}
echo "\n";
}

> ?>

> </xml>

that line is bad in your xml

My output, after I fixed the xml:

hostname=hostname02
offset=0
ntpd=ERROR: timeout

hostname=hostname01
offset=0
ntpd=running

Rgds

Denis McMahon
[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: Fri Nov 29 18:17:03 GMT 2024

Total time taken to generate the page: 0.31779 seconds