php xml DOM/xpath how to reference child nodes by name within foreach loop? [message #171605] |
Tue, 11 January 2011 22:43 |
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 |
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 |
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 #171669 is a reply to message #171605] |
Sat, 15 January 2011 00:37 |
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)
|
|
|