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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.