Jump to content

Using PHP - How to create a variable in XML?


Chrex
 Share

Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...