Fr3nch13/CakePHP Utilities

Xml
in package

XML handling for CakePHP.

The methods in these classes enable the datasources that use XML to work.

Table of Contents

Methods

build()  : SimpleXMLElement|DOMDocument
Initialize SimpleXMLElement or DOMDocument from a given XML string, file path, URL or array.
fromArray()  : SimpleXMLElement|DOMDocument
Transform an array into a SimpleXMLElement
loadHtml()  : SimpleXMLElement|DOMDocument
Parse the input html string and create either a SimpleXmlElement object or a DOMDocument.
toArray()  : array<string|int, mixed>
Returns this XML structure as an array.
_createChild()  : void
Helper to _fromArray(). It will create children of arrays
_fromArray()  : void
Recursive method to create children from array
_loadXml()  : SimpleXMLElement|DOMDocument
Parse the input data and create either a SimpleXmlElement object or a DOMDocument.
_toArray()  : void
Recursive method to toArray
load()  : SimpleXMLElement|DOMDocument
Parse the input data and create either a SimpleXmlElement object or a DOMDocument.

Methods

build()

Initialize SimpleXMLElement or DOMDocument from a given XML string, file path, URL or array.

public static build(object|array<string|int, mixed>|string $input[, array<string, mixed> $options = [] ]) : SimpleXMLElement|DOMDocument

Usage:

Building XML from a string:

$xml = Xml::build('<example>text</example>');

Building XML from string (output DOMDocument):

$xml = Xml::build('<example>text</example>', ['return' => 'domdocument']);

Building XML from a file path:

$xml = Xml::build('/path/to/an/xml/file.xml', ['readFile' => true]);

Building XML from a remote URL:

use Cake\Http\Client;

$http = new Client();
$response = $http->get('http://example.com/example.xml');
$xml = Xml::build($response->body());

Building from an array:

 $value = [
     'tags' => [
         'tag' => [
             [
                 'id' => '1',
                 'name' => 'defect'
             ],
             [
                 'id' => '2',
                 'name' => 'enhancement'
             ]
         ]
     ]
 ];
$xml = Xml::build($value);

When building XML from an array ensure that there is only one top level element.

Options

  • return Can be 'simplexml' to return object of SimpleXMLElement or 'domdocument' to return DOMDocument.
  • loadEntities Defaults to false. Set to true to enable loading of <!ENTITY definitions. This is disabled by default for security reasons.
  • readFile Set to true to enable file reading. This is disabled by default to prevent local filesystem access. Only enable this setting when the input is safe.
  • parseHuge Enable the LIBXML_PARSEHUGE flag.

If using array as input, you can pass options from Xml::fromArray.

Parameters
$input : object|array<string|int, mixed>|string

XML string, a path to a file, a URL or an array

$options : array<string, mixed> = []

The options to use

Tags
throws
XmlException
Return values
SimpleXMLElement|DOMDocument

SimpleXMLElement or DOMDocument

fromArray()

Transform an array into a SimpleXMLElement

public static fromArray(object|array<string|int, mixed> $input[, array<string, mixed> $options = [] ]) : SimpleXMLElement|DOMDocument

Options

  • format If create children ('tags') or attributes ('attributes').
  • pretty Returns formatted Xml when set to true. Defaults to false
  • version Version of XML document. Default is 1.0.
  • encoding Encoding of XML document. If null remove from XML header. Defaults to the application's encoding
  • return If return object of SimpleXMLElement ('simplexml') or DOMDocument ('domdocument'). Default is SimpleXMLElement.

Using the following data:

$value = [
   'root' => [
       'tag' => [
           'id' => 1,
           'value' => 'defect',
           '@' => 'description'
        ]
    ]
];

Calling Xml::fromArray($value, 'tags'); Will generate:

<root><tag><id>1</id><value>defect</value>description</tag></root>

And calling Xml::fromArray($value, 'attributes'); Will generate:

<root><tag id="1" value="defect">description</tag></root>

Parameters
$input : object|array<string|int, mixed>

Array with data or a collection instance.

$options : array<string, mixed> = []

The options to use.

Tags
throws
XmlException
Return values
SimpleXMLElement|DOMDocument

SimpleXMLElement or DOMDocument

loadHtml()

Parse the input html string and create either a SimpleXmlElement object or a DOMDocument.

public static loadHtml(string $input[, array<string, mixed> $options = [] ]) : SimpleXMLElement|DOMDocument
Parameters
$input : string

The input html string to load.

$options : array<string, mixed> = []

The options to use. See Xml::build()

Tags
throws
XmlException
Return values
SimpleXMLElement|DOMDocument

toArray()

Returns this XML structure as an array.

public static toArray(SimpleXMLElement|DOMDocument|DOMNode $obj) : array<string|int, mixed>
Parameters
$obj : SimpleXMLElement|DOMDocument|DOMNode

SimpleXMLElement, DOMDocument or DOMNode instance

Tags
throws
XmlException
Return values
array<string|int, mixed>

Array representation of the XML structure.

_createChild()

Helper to _fromArray(). It will create children of arrays

protected static _createChild(array<string, mixed> $data) : void
Parameters
$data : array<string, mixed>

Array with information to create children

_fromArray()

Recursive method to create children from array

protected static _fromArray(DOMDocument $dom, DOMDocument|DOMElement $node, array<string|int, mixed> &$data, string $format) : void
Parameters
$dom : DOMDocument

Handler to DOMDocument

$node : DOMDocument|DOMElement

Handler to DOMElement (child)

$data : array<string|int, mixed>

Array of data to append to the $node.

$format : string

Either 'attributes' or 'tags'. This determines where nested keys go.

Tags
throws
XmlException

_loadXml()

Parse the input data and create either a SimpleXmlElement object or a DOMDocument.

protected static _loadXml(string $input, array<string, mixed> $options) : SimpleXMLElement|DOMDocument
Parameters
$input : string

The input to load.

$options : array<string, mixed>

The options to use. See Xml::build()

Tags
throws
XmlException
Return values
SimpleXMLElement|DOMDocument

_toArray()

Recursive method to toArray

protected static _toArray(SimpleXMLElement $xml, array<string, mixed> &$parentData, string $ns, array<string|int, string> $namespaces) : void
Parameters
$xml : SimpleXMLElement

SimpleXMLElement object

$parentData : array<string, mixed>

Parent array with data

$ns : string

Namespace of current child

$namespaces : array<string|int, string>

List of namespaces in XML

load()

Parse the input data and create either a SimpleXmlElement object or a DOMDocument.

protected static load(string $input, array<string, mixed> $options, Closure $callable) : SimpleXMLElement|DOMDocument
Parameters
$input : string

The input to load.

$options : array<string, mixed>

The options to use. See Xml::build()

$callable : Closure

Closure that should return SimpleXMLElement or DOMDocument instance.

Tags
throws
XmlException
Return values
SimpleXMLElement|DOMDocument

        
On this page

Search results