Web Hosting Talk







View Full Version : XML template value replacement using PHP?


eger
04-14-2009, 12:00 PM
I know what I want to do.. but I am having trouble getting started with it. So here it is.

I want to provide an XML template such as:

<tree>
<value1>$VALUE1$</value1>
<newtree>
<value2>$VALUE2$</value2>
</newtree>
</tree>

Then when given the XML:

<tree>
<value1>150</value1>
<newtree>
<value2>20</value2>
</newtree>
</tree>

I want to parse the values in variables (such as $value1 and $value2).

I have figured out how to get the XML into a PHP array. But this is not quite the result I am looking for.

Any ideas how to approach this?

Jamie Edwards
04-14-2009, 06:32 PM
<?php

$xml = "
<tree>
<value1>$VALUE1$</value1>
<newtree>
<value2>$VALUE2$</value2>
</newtree>
</tree>
";

// Load a SimpleXMLObject from the given XML
$tree = simplexml_load_file($xml);

// $tree = root node (<tree></tree>), so the contents of $tree->value1 = <tree><value1>$VALUE1$</value1></tree>

echo "Value 1: " . $tree->value1;
echo "Value 2: " . $tree->newtree->value2;

?>

Does this get you started?