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 #171606 is a reply to message #171605] Tue, 11 January 2011 23:21 Go to previous messageGo to previous message
inetquestion is currently offline  inetquestion
Messages: 2
Registered: January 2011
Karma:
Junior Member
On Jan 11, 5:43 pm, inetquestion <inetquest...@hotmail.com> wrote:
> 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>

found a solution using a different method:

<?Php
$xmlData = new SimpleXMLElement("servers.xml", NULL, true);
foreach ($xmlData->server as $server)
{
echo $server->hostname . ",";
echo $server->machtype . ",";
echo $server->tier . ",";
echo $server->sysadmin . ",";
echo $server->offset . ",";
echo $server->ntpd . ",\n";
}
?>
[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: Sun Oct 20 13:44:14 GMT 2024

Total time taken to generate the page: 0.05022 seconds