I am creating a PHP script to return the file structure of a directory as an XML document. I have used simpleXML to do this, and the script is working, but the response is nested in HTML tags.
PHP file:
<!DOCTYPE HTML>
<html>
<body>
<?php
$xml_string = <<<XML
<?xml version='1.0' standalone='yes'?>
<directory>
</directory>
XML;
$directory=new SimpleXMLElement($xml_string);
Code creating the file structure.
echo $directory->asXML();
?>
</body>
</html>
The response is as follows:
<!DOCTYPE HTML>
<html>
<body>
<?xml version='1.0' standalone='yes'?>
<directory>
XML tags created by script
</directory>
</body>
</html>
I am wondering why the XML is inside an HTML document. I can’t get any javascript to access the XML elements.
var xmlhttp = new XMLHttpRequest();
var xmlDoc=xmlhttp.responseXML.documentElement.getElementsByTagName("folder");
document.getElementById("myDiv").innerHTML = xmlDoc[0].firstChild.nodeValue;
(snippets of the Javascript code used)
I get an error: ‘xmlhttp.responeXML is undefined’
Is there any way of interpreting the response or not having the XML nested inside HTML?
It’s because the code generating the xml is surrounded by html. Remove the html parts and you should be fine.
Remove this:
And this: