build()
build(string|array $input, array $options = array()) : \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');
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 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.parseHugeEnable theLIBXML_PARSEHUGEflag.
If using array as input, you can pass options from Xml::fromArray.
Parameters
| string|array | $input | XML string, a path to a file, a URL or an array |
| array | $options | The options to use |
Throws
- \Cake\Utility\Exception\XmlException
Returns
\SimpleXMLElement|\DOMDocument —SimpleXMLElement or DOMDocument