Better way to load evaluated PHP XML file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Better way to load evaluated PHP XML file?

Post by TildeHash »

Is there a better way to do this?

Code: Select all

<?php
	$feed = simplexml_load_file('http://127.0.0.1/testxml.php');
	echo $feed->test;
?>
I'm looping back in order to load a PHP file that generates XML.

Example: "testxml.php" file contains something like:

Code: Select all

<?php
	header('Content-Type: application/xml');
	echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>

<content>
	<test><?php for ($i = 0; $i < 10; $i++) echo $i . ''; ?></test>
</content>
The 127.0.0.1 is adding unnecessary load time, if I'm not mistaken.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Better way to load evaluated PHP XML file?

Post by requinix »

You could

Code: Select all

ob_start();
include "testxml.php";
$feed = new SimpleXMLElement(ob_get_clean()); // or simplexml_load_string()
What would be even better is if you moved the <test> generation stuff into a function so you could use it in both places without caring about whether the content it returns goes into XML or gets processed by more code.
User avatar
TildeHash
Forum Commoner
Posts: 43
Joined: Fri Jul 16, 2010 7:17 am
Location: Apple Valley, California

Re: Better way to load evaluated PHP XML file?

Post by TildeHash »

requinix wrote:You could

Code: Select all

ob_start();
include "testxml.php";
$feed = new SimpleXMLElement(ob_get_clean()); // or simplexml_load_string()
What would be even better is if you moved the <test> generation stuff into a function so you could use it in both places without caring about whether the content it returns goes into XML or gets processed by more code.
That works! :drunk: Many thanks :D
Post Reply