Jump to content

Generating an XML "file" from ProcessWire


Recommended Posts

My latest ProcessWire project is converting a Flash (soon to be HTML5) site that is fed by a manually-edited XML file into one fed by a dynamically-generated XML file. Everything is going great with the XML generation; PW's markup-agnostic output is in top form here. :)

I have a somewhat strange approach for this new site; it is simultaneously loading some content from the XML in PHP (for SEO), and then loading other content in JavaScript (for speed and flexibility). So it loads it once on the server and once on the client. However, I've run into a simple problem I can't seem to solve: I can load the home XML page in JS without problems, but when I try to load it in PHP:

[pre]$xmldata = simplexml_load_file('sitecontent/');[/pre]

The server says the file is empty. Any ideas?

[edit] sitecontent/ is the ProcessWire root.

Link to comment
Share on other sites

I think that your simplexml_load_file() is attempting to load from your file system, not from http. So if you wanted to load it that way, you'd probably need to put in an http address, like this:

$data = file_get_contents("http://domain.com/sitecontent/"); 
$xmldata = simplexml_load_string($data); 

But that's kind of a lot of overhead, to have one http request launching another on the same server. Granted, it happens all the time, but ideally you'd get some caching going here so that you don't have to do an http request and simplexml on every page load.

If you can include PW's API, then I think it'd be preferable to avoid the extra http request, like this:

$home = wire('pages')->get('/'); 
$data = $home->render();
$xmldata = simplexml_load_string($data);

Then again, if you can load PW's API, you probably don't need to be dealing with SimpleXML at all. :) But if you just need to get something going quickly, either of these code examples above should work.

Link to comment
Share on other sites

Indeed, I'd rather just use PW's API to render everything instead of SimpleXML. :) The only reason I'm using it really is so that we have a clean XML source to hand off to whoever ends up developing the mobile version of the site.

When I try including the API, though, I get an exception:

[pre]Uncaught exception 'WireException' with message 'Page /newsite/2012/ may not be rendered because outputFormatting is set to false. Call $page->setOutputFormatting(true) before rendering the page.' in /home/tonicblu/public_html/newsite/2012/sitecontent/wire/modules/PageRender.module:229

Stack trace:

#0 /home/tonicblu/public_html/newsite/2012/sitecontent/wire/core/Wire.php(267): PageRender->___renderPage(Object(HookEvent))

#1 /home/tonicblu/public_html/newsite/2012/sitecontent/wire/core/Wire.php(229): Wire->runHooks(Array, Array)

#2 [internal function]: Wire->__call('renderPage', Array)

#3 /home/tonicblu/public_html/newsite/2012/sitecontent/wire/core/Wire.php(289): PageRender->renderPage('renderPage', Array)

#4 /home/tonicblu/public_html/newsite/2012/sitecontent/wire/core/Wire.php(229): Wire->runHooks(Object(HookEvent))

#5 [internal function]: Wire->__call('render', Array)

#6 /home/tonicblu/public_html/newsite/2012/index.php(20): Page->render('render', Array)

#7 {main}

thrown (line 229 of /home/tonicblu/public_html/newsite/2012/sitecontent/wire/modules/PageRender.module)[/pre]


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...