Chrex 0 Posted October 1, 2014 Report Share Posted October 1, 2014 Hi guys, I try to build an xml document with php. Creating, saving and sending works fine. Just couldnt figure out how to insert a variable to my php-code, which creates the xml. <?php$xml = new DOMDocument("1.0", "utf-8";$root = $xml->createElement("ABC");$xml->appendChild($root);$var1 = $xml->createElement("Town");$var1Text = $xml->createTextNode('$townname');$var1->appendChild($varText);$BCD = $xml->createElement("Data");$BCD->appendChild($var1);$root->appendChild($BCD);$xml->formatOutput = true;echo "<xmp>". $xml->saveXML() ."</xmp>";$xml->save("energieausweis.xml") or die("Error");?> Instead of using a fix "createTextNode" I wanna use a variable. Something like "createVariableNode" (I know that this doesnt exist). Hope you guys understand me. I get a variable from a form, lets say $townname, which can be Vancouver or Paris. This variable shall be written in my xml. Depending on the selection from the form, my xml shall contain Vancouver or Paris and not just the text "$townname". Thanks a lot for your help! Quote Link to post Share on other sites
Ingolme 1,032 Posted October 1, 2014 Report Share Posted October 1, 2014 The XML language doesn't have variables. If you want the contents of a PHP variable in your XML document, just pass the variable to the function: $value = 'Some text';$textNode= $xml->createTextNode($value); When using variables in PHP you don't wrap them in quotation marks, especially not single quotes. In PHP single quotes print out literally what is between them. Double quotes print text, special characters and variables. If all you want is the value of the variable itself, then don't use any quotation marks at all. The variable has the value in it already. 1 Quote Link to post Share on other sites
Chrex 0 Posted October 1, 2014 Author Report Share Posted October 1, 2014 (edited) Thanks for the hint! I took my value from the session and did it like this: $value = $_SESSION['name']; $valueText = $xml->createTextNode($value); It works Edited October 1, 2014 by Chrex Quote Link to post Share on other sites
Ingolme 1,032 Posted October 1, 2014 Report Share Posted October 1, 2014 You could even put $_SESSION['name'] right in the function. 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.