XML handling for CakePHP.
The methods in these classes enable the datasources that use XML to work.
Helper to _fromArray(). It will create children of arrays
Recursive method to create children from array
Parse the input data and create either a SimpleXmlElement object or a DOMDocument.
Recursive method to toArray
Initialize SimpleXMLElement or DOMDocument from a given XML string, file path, URL or array.
Transform an array into a SimpleXMLElement
Parse the input data and create either a SimpleXmlElement object or a DOMDocument.
Parse the input html string and create either a SimpleXmlElement object or a DOMDocument.
Returns this XML structure as an array.
_createChild(array<string, mixed> $data): void
Helper to _fromArray(). It will create children of arrays
array<string, mixed>
$data Array with information to create children
void
_fromArray(DOMDocument $dom, DOMDocumentDOMElement $node, array $data, string $format): void
Recursive method to create children from array
DOMDocument
$dom Handler to DOMDocument
DOMDocumentDOMElement
$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.
void
Cake\Utility\Exception\XmlException
_loadXml(string $input, array<string, mixed> $options): SimpleXMLElementDOMDocument
Parse the input data and create either a SimpleXmlElement object or a DOMDocument.
string
$input The input to load.
array<string, mixed>
$options The options to use. See Xml::build()
SimpleXMLElementDOMDocument
Cake\Utility\Exception\XmlException
_toArray(SimpleXMLElement $xml, array<string, mixed> $parentData, string $ns, array<string> $namespaces): void
Recursive method to toArray
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
void
build(object|array|string $input, array<string, mixed> $options = []): SimpleXMLElementDOMDocument
Initialize SimpleXMLElement or DOMDocument from a given XML string, file path, URL or array.
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->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.
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.
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
SimpleXMLElementDOMDocument
Cake\Utility\Exception\XmlException
fromArray(object|array $input, array<string, mixed> $options = []): SimpleXMLElementDOMDocument
Transform an array into a SimpleXMLElement
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 encodingreturn
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>
object|array
$input Array with data or a collection instance.
array<string, mixed>
$options optional The options to use.
SimpleXMLElementDOMDocument
Cake\Utility\Exception\XmlException
load(string $input, array<string, mixed> $options, Closure $callable): SimpleXMLElementDOMDocument
Parse the input data and create either a SimpleXmlElement object or a DOMDocument.
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.
SimpleXMLElementDOMDocument
Cake\Utility\Exception\XmlException
loadHtml(string $input, array<string, mixed> $options = []): SimpleXMLElementDOMDocument
Parse the input html string and create either a SimpleXmlElement object or a DOMDocument.
string
$input The input html string to load.
array<string, mixed>
$options optional The options to use. See Xml::build()
SimpleXMLElementDOMDocument
Cake\Utility\Exception\XmlException
toArray(SimpleXMLElementDOMDocumentDOMNode $obj): array
Returns this XML structure as an array.
SimpleXMLElementDOMDocumentDOMNode
$obj SimpleXMLElement, DOMDocument or DOMNode instance
array
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/4.4/class-Cake.Utility.Xml.html