Class Xml

XML handling for CakePHP.

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

Namespace: Cake\Utility

Method Summary

  • _createChild() protected static

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

  • _fromArray() protected static

    Recursive method to create childs from array

  • _loadXml() protected static

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

  • _toArray() protected static

    Recursive method to toArray

  • build() public static

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

  • fromArray() public static

    Transform an array into a SimpleXMLElement

  • loadHtml() public static

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

  • toArray() public static

    Returns this XML structure as an array.

Method Detail

_createChild() protected static

_createChild(mixed $data)

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

Parameters

array $data

Array with information to create childs

_fromArray() protected static

_fromArray(mixed $dom, mixed $node, mixed $data, mixed $format)

Recursive method to create childs from array

Parameters

\DOMDocument $dom

Handler to DOMDocument

\DOMDocument|\DOMElement $node

Handler to DOMElement (child)

array $data

Array of data to append to the $node.

string $format

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

Throws

Cake\Utility\Exception\XmlException

_loadXml() protected static

_loadXml(mixed $input, mixed $options)

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

Parameters

string $input

The input to load.

array $options

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

Returns

\SimpleXMLElement|\DOMDocument

Throws

Cake\Utility\Exception\XmlException

_toArray() protected static

_toArray(mixed $xml, mixed $parentData, mixed $ns, mixed $namespaces)

Recursive method to toArray

Parameters

\SimpleXMLElement $xml

SimpleXMLElement object

array $parentData

Parent array with data

string $ns

Namespace of current child

string[] $namespaces

List of namespaces in XML

build() public static

build(mixed $input, array $options)

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

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

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

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 false to disable file reading. This is important to disable when putting user data into Xml::build(). If enabled local files will be read if they exist. Defaults to true for backwards compatibility reasons.
  • parseHuge Enable the LIBXML_PARSEHUGE flag.

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

Parameters

string|array|object $input

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

array $options optional

The options to use

Returns

\SimpleXMLElement|\DOMDocument

SimpleXMLElement or DOMDocument

Throws

Cake\Utility\Exception\XmlException

fromArray() public static

fromArray(mixed $input, mixed $options)

Transform an array into a SimpleXMLElement

Options

  • format If create childs ('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. Default is the some of application.
  • 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

array|object $input

Array with data or a collection instance.

array $options optional

The options to use.

Returns

\SimpleXMLElement|\DOMDocument

SimpleXMLElement or DOMDocument

Throws

Cake\Utility\Exception\XmlException

loadHtml() public static

loadHtml(mixed $input, mixed $options)

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

Parameters

string $input

The input html string to load.

array $options optional

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

Returns

\SimpleXMLElement|\DOMDocument

Throws

Cake\Utility\Exception\XmlException

toArray() public static

toArray(mixed $obj)

Returns this XML structure as an array.

Parameters

\SimpleXMLElement|\DOMDocument|\DOMNode $obj

SimpleXMLElement, DOMDocument or DOMNode instance

Returns

array

Array representation of the XML structure.

Throws

Cake\Utility\Exception\XmlException

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.9/class-Cake.Utility.Xml.html