class ArrayToXml (View source)

Convert PHP Array to XML

Class ArrayToXml

Properties

protected DOMDocument $document

The root DOM Document.

protected bool $replaceSpacesByUnderScoresInKeyNames

Set to enable replacing space with underscore.

Methods

__construct(array $array, string|array $rootElement = '', bool $replaceSpacesByUnderScoresInKeyNames = true, string $xmlEncoding = 'UTF-8', string $xmlVersion = '1.0')

Construct a new instance.

static string
convert(array $array, string $rootElementName = 'document', bool $replaceSpacesByUnderScoresInKeyNames = true, string $xmlEncoding = 'UTF-8', string $xmlVersion = '1.0')

Convert the given array to an xml string.

string
toXml()

Return as XML.

toDom()

Return as DOM object.

addNode(DOMElement $element, string $key, string|string[] $value)

Add node.

addCollectionNode(DOMElement $element, string|string[] $value)

Add collection node.

addSequentialNode(DOMElement $element, string|string[] $value)

Add sequential node.

bool
isArrayAllKeySequential(array|string $value)

Check if array are all sequential.

addAttributes(DOMElement $element, string[] $data)

Add attributes.

createRootElement(string|array $rootElement)

Create the root element.

Details

__construct(array $array, string|array $rootElement = '', bool $replaceSpacesByUnderScoresInKeyNames = true, string $xmlEncoding = 'UTF-8', string $xmlVersion = '1.0')

Construct a new instance.

Parameters

array $array
string|array $rootElement
bool $replaceSpacesByUnderScoresInKeyNames
string $xmlEncoding
string $xmlVersion

Exceptions

DOMException

static string convert(array $array, string $rootElementName = 'document', bool $replaceSpacesByUnderScoresInKeyNames = true, string $xmlEncoding = 'UTF-8', string $xmlVersion = '1.0')

Convert the given array to an xml string.

Parameters

array $array
string $rootElementName
bool $replaceSpacesByUnderScoresInKeyNames
string $xmlEncoding
string $xmlVersion

Return Value

string

string toXml()

Return as XML.

Return Value

string

DOMDocument toDom()

Return as DOM object.

Return Value

DOMDocument

protected addNode(DOMElement $element, string $key, string|string[] $value)

Add node.

Parameters

DOMElement $element
string $key
string|string[] $value

protected addCollectionNode(DOMElement $element, string|string[] $value)

internal  param string $key
 

Add collection node.

Parameters

DOMElement $element
string|string[] $value

protected addSequentialNode(DOMElement $element, string|string[] $value)

internal  param string $key
 

Add sequential node.

Parameters

DOMElement $element
string|string[] $value

protected bool isArrayAllKeySequential(array|string $value)

Check if array are all sequential.

Parameters

array|string $value

Return Value

bool

protected addAttributes(DOMElement $element, string[] $data)

Add attributes.

Parameters

DOMElement $element
string[] $data

protected DOMElement createRootElement(string|array $rootElement)

Create the root element.

Parameters

string|array $rootElement

Return Value

DOMElement