Xml
- class
Xml
- The Xml class allows you to transform arrays into SimpleXMLElement orDOMDocument objects, and back into arrays again.
- static
Cake\Utility\Xml::
build
($input, array $options = []) - You can load XML-ish data using
Xml::build()
. Depending on your$options
parameter, this method will return a SimpleXMLElement (default)or DOMDocument object. You can useXml::build()
to build XMLobjects from a variety of sources. For example, you can load XML fromstrings:
- // Local file
- $xml = Xml::build('/home/awesome/unicorns.xml');
You can also build Xml objects using an array:
- $data = [
- 'post' => [
- 'id' => 1,
- 'title' => 'Best post',
- 'body' => ' ... '
- ]
- ];
- $xml = Xml::build($data);
If your input is invalid, the Xml class will throw an exception:
- $xmlString = 'What is XML?';
- try {
- $xmlObject = Xml::build($xmlString); // Here will throw an exception
- throw new InternalErrorException();
- }
Note
DOMDocument and implement different API’s.Be sure to use the correct methods on the object you request from Xml.
By default entity loading and huge document parsing are disabled. These modescan be enabled with the loadEntities
and parseHuge
options respectively.
New in version 3.7.0.
toArray($obj);
- Converting XML strings into arrays is simple with the Xml class as well. Bydefault you’ll get a SimpleXml object back:
- $xmlString = '<?xml version="1.0"?><root><child>value</child></root>';
- $xmlArray = Xml::toArray(Xml::build($xmlString));
If your XML is invalid a Cake\Utility\Exception\XmlException
will be raised.
- $xmlArray = ['root' => ['child' => 'value']];
- // You can use Xml::build() too.
- $xmlObject = Xml::fromArray($xmlArray, ['format' => 'tags']);
- $xmlString = $xmlObject->asXML();
Your array must have only one element in the “top level” and it can not benumeric. If the array is not in this format, Xml will throw an exception.Examples of invalid arrays:
- // Top level with numeric key
- [
- ['key' => 'value']
- ];
- // Multiple keys in top level
- [
- 'key1' => 'first value',
- 'key2' => 'other value'
- ];
The content of $xmlString
will be:
- <?xml version="1.0"?>
- <project id="1">Value of project<name>Name of project, as tag</name></project>
To use XML Namespaces, create a key in your array with the name xmlns:
in a generic namespace or input the prefix xmlns:
in a custom namespace. Seethe samples:
- $xmlArray = [
- 'xmlns:' => 'https://cakephp.org',
- 'child' => 'value'
- ]
- ];
- $xml1 = Xml::fromArray($xmlArray);
- $xmlArray(
- 'root' => [
- 'tag' => [
- 'xmlns:pref' => 'https://cakephp.org',
- 'pref:item' => [
- 'item 1',
- 'item 2'
- ]
- ]
- ]
- );
- $xml2 = Xml::fromArray($xmlArray);
The value of $xml1
and $xml2
will be, respectively:
- <?xml version="1.0"?>
- <root xmlns="https://cakephp.org"><child>value</child>
- <?xml version="1.0"?>
- <root><tag xmlns:pref="https://cakephp.org"><pref:item>item 1</pref:item><pref:item>item 2</pref:item></tag></root>
Creating a Child
After you have created your XML document, you just use the native interfaces foryour document type to add, remove, or manipulate child nodes:
After manipulating your XML using SimpleXMLElement or DomDocument you canuse without a problem.