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
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.2
      • 4.2
      • 4.1
      • 4.0
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Hash
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml

Class Xml

XML handling for Cake.

The methods in these classes enable the datasources that use XML to work.

Package: Cake\Utility
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: Cake/Utility/Xml.php

Method Summary

  • _createChild() protected static
    Helper to _fromArray(). It will create childs of arrays
  • _fromArray() protected static
    Recursive method to create childs 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
  • toArray() public static
    Returns this XML structure as a array.

Method Detail

_createChild() protected static ¶

_createChild( array $data )

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

Parameters
array $data
Array with informations to create childs

_fromArray() protected static ¶

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

Recursive method to create childs from array

Parameters
DOMDocument $dom
Handler to DOMDocument
DOMElement $node
Handler to DOMElement (child)
array $data
Array of data to append to the $node.
string $format
Either 'attribute' or 'tags'. This determines where nested keys go.
Throws
XmlException

_loadXml() protected static ¶

_loadXml( string $input , array $options )

Parse 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|DOMDocument.

_toArray() protected static ¶

_toArray( SimpleXMLElement $xml , array $parentData , string $ns , array $namespaces )

Recursive method to toArray

Parameters
SimpleXMLElement $xml
SimpleXMLElement object
array $parentData
Parent array with data
string $ns
Namespace of current child
array $namespaces
List of namespaces in XML

build() public static ¶

build( string|array $input , array $options = array() )

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>', array('return' => 'domdocument'));

Building XML from a file path:

$xml = Xml::build('/path/to/an/xml/file.xml');

Building from a remote URL:

$xml = Xml::build('http://example.com/example.xml');

Building from an array:

{{{ $value = array( 'tags' => array( 'tag' => array( array( 'id' => '1', 'name' => 'defect' ), array( '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.
  • If using array as input, you can pass options from Xml::fromArray.
Parameters
string|array $input
XML string, a path to a file, an URL or an array
array $options optional array()
The options to use
Returns
SimpleXMLElement|DOMDocument
SimpleXMLElement or DOMDocument
Throws
XmlException

fromArray() public static ¶

fromArray( array $input , array $options = array() )

Transform an array into a SimpleXMLElement

Options

  • format If create childs ('tags') or attributes ('attribute').
  • version Version of XML document. Default is 1.0.
  • encoding Encoding of XML document. If null remove from XML header. Default is the some of application.
  • return If return object of SimpleXMLElement ('simplexml') or DOMDocument ('domdocument'). Default is SimpleXMLElement.

Using the following data:

{{{ $value = array( 'root' => array( 'tag' => array( '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, 'attribute'); Will generate:

<root><tag id="1" value="defect">description</tag></root>

Parameters
array $input
Array with data
array $options optional array()
The options to use
Returns
SimpleXMLElement|DOMDocument
SimpleXMLElement or DOMDocument
Throws
XmlException

toArray() public static ¶

toArray( SimpleXMLElement|DOMDocument|DOMNode $obj )

Returns this XML structure as a array.

Parameters
SimpleXMLElement|DOMDocument|DOMNode $obj
SimpleXMLElement, DOMDocument or DOMNode instance
Returns
array
Array representation of the XML structure.
Throws
XmlException
OpenHub
Rackspace
Rackspace
  • 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
  • Slack
  • Paid Support

Generated using CakePHP API Docs