PHP code example of thapp / xmlbuilder

1. Go to this page and download the library: Download thapp/xmlbuilder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thapp / xmlbuilder example snippets




use Thapp\XmlBuilder\XmlBuilder;
use Thapp\XmlBuilder\Normalizer;

$data = array(
  'foo' => 'bar',
  'node' => array(
    '@attributes' => array(
      'date' => '2013-06-06'
    ), 'some string'
  );
);

$xmlBuilder = new XmlBuilder('data');
$xmlBuilder->load($data);

// createXML accepts a boolean value weather to return a string or a DOMDocument
// Set it to `false` if you want to retreive a DOMDocument instead.

echo $xmlBuilder->createXML(true); 



$data = array('id' => 12, 'bar' => 'baz');

$xmlBuilder = new XmlBuilder('response');
$XmlBuilder->load($data);


$XmlBuilder->setAttributeMapp(array('response' => array('id')));
echo $XmlBuilder->createXML();



class DataObject
{
  protected $foo = 'bar';
  
  public $bar = 'baz';
  
  public function getFoo()
  {
    return $this->foo;
  }
}



$object = new DataObject('data');

$xmlBuilder->load($object);


echo $xmlBuilder->createXML(true);





//...

$xmlBuilder->setSingularizer(function ($name) {

  if ('entries' === $name) {
    return 'entry';
  }
  
  return $name;
});

$entries = array(
  'entries' => array(
    'foo',
    'bar',
    'baz',
  )
);

$xmlBuilder->load($entries);

echo $xmlBuilder->createXML();



// ...
$xml = $xmlBuilder->loadXML('myxmlfile.xml', false);
// or
$xml = $xmlBuilder->loadXML('<data><foo></foo></data>', true);


// ...

$xml   = $xmlBuilder->loadXML('<data><foo>bar</foo></data>', true);
$array = $xmlBuilder->toArray($xml); // array('data' => array('foo' => 'bar')); 




//...

array(
'data' => array(
	'foo' => 'bar'
	)
); 




//...

$xmlBuilder->setPluralizer(function ($name) {
	if ('entry' === $name) {
		return 'entries';
	}
}); 



// ...
array(
	'data' => array('entries' => array(
		'entry' => array('foo', 'bar')
	))
);


// ...
array(
	'data' => array(
		'entries' => array('foo', 'bar')
	)
);
xml
<data>
  <entries>
    <entry>foo</entry>
    <entry>bar</entry>
    <entry>baz</entry>
  </entries>
</data>
xml
<data>
	<entries>
		<entry>foo</entry>
		<entry>bar</entry>
	</entries>
</data>