Class Xml
XML handling for CakePHP.
The methods in these classes enable the datasources that use XML to work.
Method Summary
- 
          _createChild() protected staticHelper to _fromArray(). It will create childs of arrays 
- 
          _fromArray() protected staticRecursive method to create childs from array 
- 
          _loadXml() protected staticParse the input data and create either a SimpleXmlElement object or a DOMDocument. 
- 
          _toArray() protected staticRecursive method to toArray 
- 
          build() public staticInitialize SimpleXMLElement or DOMDocument from a given XML string, file path, URL or array. 
- 
          fromArray() public staticTransform an array into a SimpleXMLElement 
- 
          loadHtml() public staticParse the input html string and create either a SimpleXmlElement object or a DOMDocument. 
- 
          toArray() public staticReturns this XML structure as an array. 
Method Detail
_createChild() ¶ protected static
_createChild(array $data): voidHelper to _fromArray(). It will create childs of arrays
Parameters
- 
                array$data
- Array with information to create childs 
Returns
void_fromArray() ¶ protected static
_fromArray(DOMDocument $dom, DOMDocument|DOMElement $node, array $data, string $format): voidRecursive 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. 
Returns
voidThrows
Cake\Utility\Exception\XmlException_loadXml() ¶ protected static
_loadXml(string $input, array $options): SimpleXMLElement|DOMDocumentParse 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|DOMDocumentThrows
Cake\Utility\Exception\XmlException_toArray() ¶ protected static
_toArray(SimpleXMLElement $xml, array $parentData, string $ns, string[] $namespaces): voidRecursive 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 
Returns
voidbuild() ¶ public static
build(string|array|object $input, array $options = []): SimpleXMLElement|DOMDocumentInitialize 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->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
- returnCan be 'simplexml' to return object of SimpleXMLElement or 'domdocument' to return DOMDocument.
- loadEntitiesDefaults to false. Set to true to enable loading of- <!ENTITYdefinitions. This is disabled by default for security reasons.
- readFileSet 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.
- parseHugeEnable the- LIBXML_PARSEHUGEflag.
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|DOMDocumentSimpleXMLElement or DOMDocument
Throws
Cake\Utility\Exception\XmlExceptionfromArray() ¶ public static
fromArray(array|object $input, array $options = []): SimpleXMLElement|DOMDocumentTransform an array into a SimpleXMLElement
Options
- formatIf create childs ('tags') or attributes ('attributes').
- prettyReturns formatted Xml when set to- true. Defaults to- false
- versionVersion of XML document. Default is 1.0.
- encodingEncoding of XML document. If null remove from XML header. Defaults to the application's encoding
- returnIf 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|DOMDocumentSimpleXMLElement or DOMDocument
Throws
Cake\Utility\Exception\XmlExceptionloadHtml() ¶ public static
loadHtml(string $input, array $options = []): SimpleXMLElement|DOMDocumentParse 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|DOMDocumentThrows
Cake\Utility\Exception\XmlExceptiontoArray() ¶ public static
toArray(SimpleXMLElement|DOMDocument|DOMNode $obj): arrayReturns this XML structure as an array.
Parameters
- 
                SimpleXMLElement|DOMDocument|DOMNode$obj
- SimpleXMLElement, DOMDocument or DOMNode instance 
Returns
arrayArray representation of the XML structure.
Throws
Cake\Utility\Exception\XmlException