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
Switch to threaded view of this topic 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 next message
inetquestion is currently offline  inetquestion
Messages: 2
Registered: January 2011
Karma: 0
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>
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 next message
inetquestion is currently offline  inetquestion
Messages: 2
Registered: January 2011
Karma: 0
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";
}
?>
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 next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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
Re: php xml DOM/xpath how to reference child nodes by name within foreach loop? [message #171612 is a reply to message #171610] Wed, 12 January 2011 03:36 Go to previous messageGo to next message
anny2work_at_gmail_do is currently offline  anny2work_at_gmail_do
Messages: 3
Registered: December 2010
Karma: 0
Junior Member
responding to
http://www.1-script.com/forums/php-xml-DOM-xpath-how-to-reference-child-nod es-by-name-withi-article133746--7.htm
annyphp wrote:

Denis McMahon wrote:

> 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



-------------------------------------
More php xml info
http://www.phpkode.com/scripts/category/php-xml/
Re: php xml DOM/xpath how to reference child nodes by name within foreach loop? [message #171669 is a reply to message #171605] Sat, 15 January 2011 00:37 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
inetquestion 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,

You meant to say "_type_ name". Your elements do not have names.

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

The `nodeName' of a `server' element is 'server', of course.

> print $svr->getAttribute('offset');
> print $svr->getAttribute('ntpd');

`offset' and `ntpd' are _not_ attributes of the `server' element; if they
where, it would look as follows:

<server … offset="0" ntpd="running">

</server>

and not

> <server>
> <hostname>hostname01</hostname>
> <offset>0</offset>
> <ntpd>running</ntpd>
> </server>

You are looking for child/descendant elements and their *text content*,
which can be easily retrieved (assuming a `server' element only has ever one
descendant of that type):

print $srv->getElementsByTagName('offset')->item(0)->textContent;
print $srv->getElementsByTagName('ntpd')->item(0)->textContent;

Or if you want to use XPath:

$xpath = new DOMXPath($xmlDOM);
// …
$result = $xpath->evaluate('.//offset', $server);
print $result->item(0)->textContent;

(Those two approaches are equivalent, but XPath has a more efficient way if
you only want to consider child elements: './offset'.)

This really has little to do with PHP:

<http://www.w3.org/TR/DOM-Level-3-Core/>
<http://www.w3.org/TR/xpath/>


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Tue Nov 26 06:01:45 GMT 2024

Total time taken to generate the page: 0.02618 seconds