Need to write XML using PHP - how?

I've got this basic code.

<chart lowerLimit='0' upperLimit='100' caption='Revenue' subcaption='US $ (1,000s)' numberPrefix='$' numberSuffix='K' showValue='1' >
      <color minValue='0' maxValue='50' color='A6A6A6'/>
      <color minValue='50' maxValue='75' color='CCCCCC'/> 
      <color minValue='75' maxValue='100' color='E1E1E1'/> 

it's used from fusionwidgets and there's no documentation on how to write this in PHP.

can anybody advise?


There is complete example with to produce exactly the same XML output like you posted.

$writer = new XMLWriter();  
   $writer->writeAttribute('lowerLimit', '0');  
   $writer->writeAttribute('upperLimit', '100');  
   $writer->writeAttribute('caption', 'Revenue');  
   $writer->writeAttribute('subcaption', 'US $ (1,000s)');  
   $writer->writeAttribute('numberPrefix', '$');  
   $writer->writeAttribute('numberSuffix', 'K');  
   $writer->writeAttribute('showValue', '1');  
         $writer->writeAttribute('minValue', '0');  
         $writer->writeAttribute('maxValue', '50'); 
         $writer->writeAttribute('color', 'A6A6A6'); 
         $writer->writeAttribute('minValue', '50');  
         $writer->writeAttribute('maxValue', '75'); 
         $writer->writeAttribute('color', 'CCCCCC'); 
         $writer->writeAttribute('minValue', '75');  
         $writer->writeAttribute('maxValue', '100'); 
         $writer->writeAttribute('color', 'E1E1E1'); 

My favorite way to write XML files is XMLWriter - . It's very powerfull and simple to use.

           $writer = new XMLWriter();  
           $writer->writeElement('user_id', 3);  
           $writer->writeElement('msg_count', 11);  
           $writer->writeAttribute('category', 'test');  

And that piece of code will produce the following XML:

<?xml version="1.0" encoding="UTF-8"?>
 <msg category="test"/>


Personally, I prefer DOM for working with XML.

You might want to clearify your question. Right now, it's hard to tell what your problem is.

With FluidXML you can generate your XML in this way.

$chart = fluidxml('chart');

$chart->attr('lowerLimit', 0)
      ->attr('upperLimit', 100)
      ->add('value', 78.9)
      ->add('target', 80)
          ->add('color', ['minValue' => 0, 'maxValue' => 50, ...])
          ->add('color', ['minValue' => 50, 'maxValue' => 75, ...])
          ->add('color', ['minValue' => 75, 'maxValue' => 100, ...]);

SimpleXML, wich is built into PHP is the most simple solution for writing (and parsing) XML.

As mentioned before, setIndent sets indentation on and should be used like this:


if you want to set the indentation size to a different one than the default (2 spaces), you can use setIndentString:

$writer->setIndentString("    ");

Need Your Help

How to hide audio file inside image

c# image byte steganography

I'm working on a stenography program where i need to hide audio file inside image.