GithubHelp home page GithubHelp logo

xmlbuilder's Introduction

This repository is no longer maintained.
Project has moved over here

xml builder utility

Build xml structures from arrays or objects, convert xml structures to array data structures.

Build Status

Installation

Using composer

Add thapp\xmlbuilder to your composer.json file.

"require": {
	"php":">=5.3.7"
	"thapp/xmlbuilder": "v0.1.*"
}

Run composer update or composer install (if this is a clean composer project)

Usage

Create xml from array

<?php

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); 

prints:

<data>
  <foo>bar</foo>
  <node date="2013-06-06">some string</node>
</data>

XmlBuilder by defaul will create attributes on a dom node by itself as long as your key name statrts with '@', however @attributes expects an array of key value pairs wereas a key like @key would accept only scalar values (string, int, float, or boolean).

Map keys to become attributes

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

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


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

Prints:

<response id="12">
  <bar>baz</bar>
</response>

Create xml from an Object

<?php

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

XmlBuilder's Normalizer Object is aware of the getter methods of an object

$object = new DataObject('data');

$xmlBuilder->load($object);


echo $xmlBuilder->createXML(true);

prints:

 
 <data>
  <foo>bar</foo>
  <bar>baz</bar>
 </data>

Singularize child names

<?php

//...

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

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

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

$xmlBuilder->load($entries);

echo $xmlBuilder->createXML();

prints:

<data>
  <entries>
    <entry>foo</entry>
    <entry>bar</entry>
    <entry>baz</entry>
  </entries>
</data>

Loading xml strings and files

XmlBuilder let you load xml strings or files quite easily. The loadXML method accepts 3 arguments:

  • (string)xml: the xml source. can be a xml string or filename
  • (bool)sourceIsString: the xml source is a xml string or a file
  • (bool)simpleXml: return an instance of \Thapp\XmlBuilder\Dom\SimpleXMLElementinstead of \Thapp\XmlBuilder\Dom\DOMDocument
<?php
// ...
$xml = $xmlBuilder->loadXML('myxmlfile.xml', false);
// or
$xml = $xmlBuilder->loadXML('<data><foo></foo></data>', true);

To array conversion

<?php
// ...

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

results:

<?php

//...

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

The array conversion is alos aware of singulars and plurals. Just like the setSingularizer method you can call setPluralizer

<?php

//...

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

Given a xml structure like

<data>
	<entries>
		<entry>foo</entry>
		<entry>bar</entry>
	</entries>
</data>

the resulting array, without pluralizer set would look like this

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

with pluralizer

<?php
// ...
array(
	'data' => array(
		'entries' => array('foo', 'bar')
	)
);

Documentation will be updated shortly.

xmlbuilder's People

Contributors

iwyg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.