I'm trying to create an XML file on my local server from a PHP array using DOMDocument, but I'm facing some problem, that is, a file doesn't want to create itself from my code.
I'm stuck and don't know what to do next to create that file, so any advice is more than welcome.
Below I attach my existing code, which is two files, first HTML with simple form and second PHP script:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="generetingXMLFile.php" method="post" target="_blank">
<div id="row">
<span>1</span>
<input type="text" size="2" name="numbers[]">
<input type="text" name="nicknames[]">
</div>
<div id="row">
<span>2</span>
<input type="text" size="2" name="numbers[]">
<input type="text" name="nicknames[]">
</div>
<div id="row">
<span>File's name:</span>
<input type="text" name="fileName">
</div>
<input type="submit">
</form>
</body>
</html>
And the PHP script look like this.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<?php
$numbers = $_POST["numbers"];
$nicknames = $_POST["nicknames"];
$ac = array_combine($numbers, $nicknames);
print_r ($ac);
$fileName = $_POST["fileName"];
$xml = new DOMDocument("1.0", "UTF-8");
$xml -> preserveWhiteSpace = false;
$xml -> formatOutput = true;
$xmlTeam = $xml -> createElement("Team");
foreach ($ac as $key => $value) {
$xmlPlayer = $xml -> createElement("Player");
$xmlNumber = $xml -> createElement("Number", $key);
$xmlNickname = $xml -> createElement("Nickname", $value);
$xmlPlayer -> appendChild($xmlNumber);
$xmlPlayer -> appendChild($xmlNickname);
$xmlTeam -> appendChild($xmlPlayer);
}
$xml -> appendChild($xmlTeam);
$xml -> save($fileName.".xml");
if ($xml -> save($fileName.".xml") !== false) {
echo "OK!";
}
else {
echo "Error.";
}
echo "<p>".$xml -> saveXML()."</p>";
?>
</body>
</html>
So: Print_r
statement prints correctly, but if
statement echoes "Error.", and $xml -> saveXML()
echoes only nodeValues
.
So as you can see the problem is about my DOMDocument
statements in my opinion, but I can be wrong, because I'm still a newbie when it comes to programming, so I try to learn from every tip you are willing to provide.