views:

225

answers:

3

My XML file look like this:

<?xml versiion="1.0" encoding "utf-8"?>

<graph caption='chart' xAxisName='songs' yAxisName='votes' showNames='0' decimalPrecision='0' formatNumberScale='0'>
 <set name='song name' value='1233' color='AFD8F8' />
 <set name='song name' value='857' color='F6BD0F' />
 <set name='song name' value='671' color='8BBA00' />
 <set name='song name' value='494' color='FF8E46' />
 <set name='song name' value='761' color='008E8E' />
 <set name='song name' value='960' color='D64646' />
 <set name='song name' value='629' color='8E468E' />
 <set name='song name' value='622' color='588526' />
 <set name='song name' value='376' color='B3AA00' />
 <set name='song name' value='494' color='008ED6' />
 <set name='song name' value='761' color='9D080D' />
 <set name='song name' value='960' color='A186BE' />
</graph>

Can I use variables in this file in the value='$variable'???

help!!!

A: 

Of course you can:

<set name='song name' value='<?php echo $variable;?>' color='588526' />
Emil Vikström
No you can't if the XML file is not parsed with PHP - if the file is a raw .xml file, then this will obviously fail if the server is not set up to handle .xml's through PHP.
Tatu Ulmanen
He didn't say it was a .xml file, did he? =/
Emil Vikström
Thank you all very much - The problem solved!!! (-:Emil, yours was the most elegant and simple, thanks...
Tomer
A: 

XML is a text file. You can put whatever text you want into it, but variables don't make sense in this context.

What are you using the XML file for?

Oded
@Oded, apparently he's using the XML to create a flash graph dynamically, using for example this library: <http://www.maani.us/xml_charts/>
Tatu Ulmanen
+1  A: 

Yes, you can use PHP variables if the XML is handled through PHP. In simple terms, just change the extension from .xml to .php, set the content type to xml using header('Content-type: text/xml'). Then you can use the file as it was a normal PHP file:

<?php header('Content-type: text/xml'); echo '<?'; ?>xml version="1.0" encoding "utf-8"<?php echo '?>'; ?>

<graph caption='chart' xAxisName='songs' yAxisName='votes' showNames='0' decimalPrecision='0' formatNumberScale='0'>
<?php foreach($songs as $song): ?>
    <set name='<?php echo $song['name']; ?>' value='<?php echo $song['value']; ?>' color='<?php echo $song['color']; ?>' />
<?php endforeach; ?>
</graph>
Tatu Ulmanen
Not if `short_tags` is enabled in the PHP engine.. You need to `<?php echo "<?"; ?>xml [...]`
chelmertz
@chelmertz, you're right, I've updated my answer.
Tatu Ulmanen