Download the PHP package pdeans/xml-builder without Composer
On this page you can find all versions of the php package pdeans/xml-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pdeans/xml-builder
More information about pdeans/xml-builder
Files in pdeans/xml-builder
Package xml-builder
Short Description Simple and lightweight library to make generating XML a breeze.
License MIT
Informations about the package xml-builder
Easy XML Builder
Simple and lightweight library to make generating XML a breeze.
Installation
Install via Composer.
Usage
The XML builder library extends PHP's XMLWriter extension. All XMLWriter object oriented API properties and methods are available for each XML builder instance.
First, instantiate a new XML builder class object:
The create
method is used to generate an xml tag. The create
method takes the name of the root element as the first argument, and an associative array consisting of the data to build the root attribute elements and/or child elements as the second argument.
Here is a simple example:
This will produce the following xml:
Parent/Child Elements
Notice how the array key-values function under the @tags
array from the above example. The keys represent the xml element names, and the values represent the xml element values. Child tags can also be nested following this pattern with the parent element represented by the array key, and the array value consisting of an array of the child elements as key-value pairs. This pattern can be repeated as needed to nest subsequent child elements.
Element Value Helpers
The cdata
helper method can be used to wrap an element value in a <![CDATA[]]>
tag, while the decimal
helper method can be used to format a decimal number into a standard decimal format, rounding to 2 decimals by default and stripping out commas. The decimal
helper method accepts an optional second parameter to set the precision.
Reserved Keys
The @tags
key represents one of 3 reserved keys (each containing shortcut key counterparts) that the xml builder uses to parse and generate the xml. The reserved keys are as follows:
@attributes Key
Shortcut: @a
The @attributes
key is used to create xml element attributes. The @a
key is also supported as a shortcut for the @attributes
key.
Examples:
XML Produced:
@tags Key
Shortcut: @t
The @tags
key accepts an associative array of data to build the root element's children. The @t
key is also supported as a shortcut for the @tags
key.
Examples:
XML Produced:
@value Key
Shortcut: @v
The @value
key explicitly sets an xml element value. Generally, this is only required on xml elements that require both attributes and a value to be set. The @v
key is also supported as a shortcut for the @value
key.
Examples:
XML Produced:
Note that the @tags
key is used on the first level only of the associative array of tag data, as it represents the child tag data, while the other two reserved keys can be used on any sub-level throughout the associative array.
Repeated Tags
Sometimes repeated tags are used in xml, which does not play nice with associative array key-value pairs. To circumvent this, the element name is still passed as the array key, however, the array value consists of a sequential array of arrays with the tag data.
XML Produced:
Self-closing Tags
To generate a self-closing element without attributes, pass a value of null as the array value.
XML Produced:
All versions of xml-builder with dependencies
ext-xmlwriter Version *