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 #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:
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)
[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: Tue Nov 26 07:29:26 GMT 2024

Total time taken to generate the page: 0.04935 seconds