Jump to content

Help with SimpleXML traversal


mpsn

Recommended Posts

Hi, I don't know why the foreach loop doesn't output the text node content of <toFirstName> and <toLastName>?

 

please see email.xml file below:

  <?xml version="1.0" encoding="ISO-8859-1"?>
<email>
<message>
	<to>
		<toFirstName>Tove</toFirstName>
		<toLastName toType="common" style="swag">Smith</toLastName>
	</to>
	<from>
		<fromFirstName>Jani</fromFirstName>
		<fromLastName fromType="unique">Dravison</fromLastName>
	</from>
	<heading>Reminder</heading>
	<body>
		<prologue>Tis the night before Xmas...</prologue>
		<paragraph1>Don't forget me this weekend!</paragraph1>
		<paragraph2>Jump the gun!</paragraph2>
		<epilogue>The end of the world.</epilogue>
	</body>
</message>
</email>

 

Please see script below:

=================

<?php
$simpleXMLObj=simplexml_load_file("email.xml");

foreach($simpleXMLObj->message[0]->to[0] AS $curDOM) {
print $curDOM->toFirstName->getName."<br />".$curDOM->toLastName->getName."<br />";
?>

 

Please help me out, I would really appreciate it!  :'(

Link to comment
https://forums.phpfreaks.com/topic/249354-help-with-simplexml-traversal/
Share on other sites

Hi, I have a new problem. It should  be simple, but I can't get it to output the text nodes that belong to the element node: to:

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<email>
<message>
	<to>
		<toFirstName>Tove</toFirstName>
		<toLastName toType="common" style="swag">Smith</toLastName>
	</to>
	<from>
		<fromFirstName>Jani</fromFirstName>
		<fromLastName fromType="unique">Dravison</fromLastName>
	</from>
	<heading>Reminder</heading>
	<body>
		<prologue>Tis the night before Xmas...</prologue>
		<paragraph1>Don't forget me this weekend!</paragraph1>
		<paragraph2>Jump the gun!</paragraph2>
		<epilogue>The end of the world.</epilogue>
	</body>
</message>
</email>

 

here is script:

=========

foreach($simpleXMLobj->to(0)->children() AS $child) {
print "cur child: $child <br />";
}

 

Please help, I'd appreciate it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.