Chrex Posted October 1, 2014 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! Link to comment Share on other sites More sharing options...
Ingolme Posted October 1, 2014 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 Link to comment Share on other sites More sharing options...
Chrex Posted October 1, 2014 Author 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 Link to comment Share on other sites More sharing options...
Ingolme Posted October 1, 2014 Share Posted October 1, 2014 You could even put $_SESSION['name'] right in the function. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now