elemelas;10989794 wrote:
However, php is not parsing the variables' values to XML.
This has nothing to do with XML, only how PHP treat strings.
$v = 3;
echo 'I have $v';
echo "I have $v";
echo 'I have ' . $v;
elemelas;10989794 wrote:
$newItem->appendChild($xml->createElement('video', 'videos/$name'));
<video>videos/$name</video>
Like I said before, this is about string interpolation, nothing else.
elemelas;10989794 wrote:
$newItem->appendChild($xml->createElement('buttonTitle', '<![CDATA[$title]]>'));
<buttonTitle><![CDATA[$title]]></buttonTitle>
What do you expect would happen if you try to
$xml->createElement('buttonDescription', '<stuff>');
Do note that any special character (such as <) has to be replaced by its character entity (that is <😉, or be placed in a CDATA section, to not be interpreted as a special character.
Also note that
$el = $xml->createElement('person');
$name = $xml->createElement('name', 'John Doe');
$age = $xml->createElement('age', '20');
$el->appendChild($name);
$el->appendChild($age);
echo $el->nodeValue;
would output
John Doe20
That is, nodeValue contains the value of the elements text nodes, nothing else. As such, the same thing applies when setting an elements nodeValue when creating the element. A text node is created and its nodeValue is set to whatever string was supplied, and this text node is appended to the newly created element.
elemelas;10989794 wrote:
So I'm wondering how do I get PHP to parse its variables to the XML?
The exact same way PHP treats variables in any other context. PHP doesn't know that you are using your variables to create an xml document, nor does it care. It will either see a variable or a string literal.
elemelas;10989794 wrote:
PS. some of the square braces got converted to < and >
No they didn't. < and > got replaced by their respective character entities. The character entities for [ and ] are [ and ].
elemelas;10989794 wrote:
does anyone know why is that and how to fix it?
You don't. But have you looked at the XML document in an XML parsing client, such as any web browser? There is a big difference between the XML code and the parsed XML document, just like there is between looking at PHP code and the results of executing a PHP script, or HTML code and a parsed HTML document.