XML - How to print all children nodes?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

XML - How to print all children nodes?

Post by McManCSU »

I am trying to print all children nodes of a parent node, but I cannot figure out how... This is what I am doing:

Code: Select all

<server>
  <xyz>
   ...
   </xyz>
  <zyx>
   ...
   </zyx>
  <abc>
   ...
   </abc>
</server>

Code: Select all

$_doc = new DOMDocument();
	$_doc->load(SERVER_PATH);

        $_serverTag = $_doc->getElementsByTagName("server");	

	foreach ($_serverTag as $tag)
	{
	    echo $tag->nodeName;
	}
All it does is output "server", and not "xyz", "zyx" or "abc", like I want it to... Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do you want it to print "xyz", "zyx", "abc"
or "server", "xyz", "zyx" , "abc"?

What if there is <server><xyz><foo></foo></xyz>.... ?
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

There will be children with children, with children, etc. How about both answers?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Here you are

Code: Select all

<?php
$xml = <<< eox
<server>
	<xyz>
		<abc>
			<def></def>
		</abc>
	</xyz>
	<xyz>
		<abc>
			<def>
				<ghi></ghi>
			</def>
		</abc>
	</xyz>
	<zyx>
	...
	</zyx>
</server>
eox;

$dom = DOMDocument::loadxml($xml);
$xpath = new DOMXPath($dom);

echo "only direct children of server : <br />\n";
$nodeset = $xpath->query('//server/*');
foreach($nodeset as $n) {
	echo 'name: ', $n->tagName, "<br />\n";
}

echo "<hr />\n";

echo "all descendants of server : <br />\n";
$nodeset = $xpath->query('//server/descendant::*');
foreach($nodeset as $n) {
	echo 'name: ', $n->tagName, "<br />\n";
}
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

Is it possible to print any attributes? ie: <xyz id="1">? Also, is it possible to format it dependent on which sub element it is?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You should consider xsl for this.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I agree that, at this point, you should use XSLT, but if you need a quick fix, the variable $n in the loop represents a node, and can be queried for attributes.
McManCSU
Forum Newbie
Posts: 22
Joined: Mon Apr 23, 2007 6:30 pm

Post by McManCSU »

Thanks for the help. I found my last answer on http://www.w3schools.com/xpath/xpath_syntax.asp

All I have to do is something like:

Code: Select all

query(//xyz[@id='1'])
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

But that doesnÄt help you with
McManCSU wrote:Also, is it possible to format it dependent on which sub element it is?
Post Reply