2

I'm trying to get the Google Store Locator to work with MYSQL and PHP and I think the first issue I'm having is that the XML is not being created successfully yet. The store locator script is somewhat complicated so I'm trying to break it down to simpler code so I can debug what exactly is going on. Below is the code I'm using to try and get a simple XML document to be generated. It's just giving me a blank screen though and no XML output. Any reason why that would be happening?

<?php
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

header("Content-type: text/xml");

$parnode->setAttribute("name", 'test');

echo $dom->saveXML();
?>
7
  • 1
    Is it really giving you a blank page? View the page source, and you just might see your XML as <?xml version="1.0"?><markers name="test"/> -- your code does work, absent any other issues creating the DOMDocument object. Commented Feb 11, 2014 at 18:28
  • Otherwise, a blank page in PHP when output is expected means a fatal error -- go look in your error log. Crank up error reporting and display errors on screen error_reporting(E_ALL); ini_set('display_errors',1); Commented Feb 11, 2014 at 18:29
  • For testing indeed, it's sometimes easier to switch to plain/text to see the output as plain XML (Unlike RSS Feeds) won't be specially rendered. Commented Feb 11, 2014 at 18:34
  • @MichaelBerkowski, I put the error logging in and got this error "Fatal error: Class 'DOMDocument' not found in". I've Googled and found that I need to install a php-xml package on the server (it's a dedicated server). I'm assuming it's tied to that? This is helping alot, thank you so far! Commented Feb 11, 2014 at 20:53
  • Yes, you'll need to install php-xml. How you go about doing that depends on your server (distribution if it's Linux, or Windows binary) Commented Feb 11, 2014 at 20:55

1 Answer 1

2

Your code as posted is correct to generate and display a new XML document via DOMDocument. When running it, I get as output:

<?xml version="1.0"?><markers name="test"/>

As always with PHP, when you get a blank page while otherwise expecting output, make sure error_reporting is turned all the way up to E_ALL and always in development, you should enable display_errors. For a quick test, set these at runtime with

error_reporting(E_ALL);
ini_set('display_errors', 1);

Since your code should work, you will be looking for errors relating to the instantiation of DOMDocument, which is supplied in the php-xml extension.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.