CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 4.4 Strawberry API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 4.4
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Auth
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
      • Crypto
      • Exception
    • Validation
    • View

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 children of arrays

  • _fromArray() protected static

    Recursive method to create children 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

  • load() protected static

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

  • 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(array<string, mixed> $data): void

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

Parameters
array<string, mixed> $data

Array with information to create children

Returns
void

_fromArray() ¶ protected static

_fromArray(DOMDocument $dom, DOMDocument|DOMElement $node, array $data, string $format): void

Recursive method to create children 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.

Returns
void
Throws
Cake\Utility\Exception\XmlException

_loadXml() ¶ protected static

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

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

Parameters
string $input

The input to load.

array<string, mixed> $options

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

Returns
SimpleXMLElement|DOMDocument
Throws
Cake\Utility\Exception\XmlException

_toArray() ¶ protected static

_toArray(SimpleXMLElement $xml, array<string, mixed> $parentData, string $ns, array<string> $namespaces): void

Recursive method to toArray

Parameters
SimpleXMLElement $xml

SimpleXMLElement object

array<string, mixed> $parentData

Parent array with data

string $ns

Namespace of current child

array<string> $namespaces

List of namespaces in XML

Returns
void

build() ¶ public static

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

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', ['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
object|array|string $input

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

array<string, mixed> $options optional

The options to use

Returns
SimpleXMLElement|DOMDocument
Throws
Cake\Utility\Exception\XmlException

fromArray() ¶ public static

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

Transform an array into a SimpleXMLElement

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
object|array $input

Array with data or a collection instance.

array<string, mixed> $options optional

The options to use.

Returns
SimpleXMLElement|DOMDocument
Throws
Cake\Utility\Exception\XmlException

load() ¶ protected static

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

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

Parameters
string $input

The input to load.

array<string, mixed> $options

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

Closure $callable

Closure that should return SimpleXMLElement or DOMDocument instance.

Returns
SimpleXMLElement|DOMDocument
Throws
Cake\Utility\Exception\XmlException

loadHtml() ¶ public static

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

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

Parameters
string $input

The input html string to load.

array<string, mixed> $options optional

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

Returns
SimpleXMLElement|DOMDocument
Throws
Cake\Utility\Exception\XmlException

toArray() ¶ public static

toArray(SimpleXMLElement|DOMDocument|DOMNode $obj): array

Returns this XML structure as an array.

Parameters
SimpleXMLElement|DOMDocument|DOMNode $obj

SimpleXMLElement, DOMDocument or DOMNode instance

Returns
array
Throws
Cake\Utility\Exception\XmlException
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs