class XML (View source)

The GenericDatabase\Helpers\Parsers\XML class provides a set of static methods for working with XML data.

It includes functionalities to check if an XML string is valid, convert data to appropriate types, decode a SimpleXMLElement object into an array or a string, and parse XML data into an array.

Example Usage:

// Check if an XML string is valid
$xmlString = "<root><element>data</element></root>";
$isValid = XML::isValidXML($xmlString);

Output: true

// Convert data to appropriate types
$data = "123";
$convertedData = XML::convertData($data);

Output: 123 (integer)

// Decode a SimpleXMLElement object into an array or a string
$xml = simplexml_load_string("<root><element>data</element></root>");
$decodedData = XML::decodeXML($xml);

Output: ['element' => 'data']

// Parse XML data into an array
$xmlData = "<root><options><option name='option1'>value1</option></options></root>";
$parsedData = XML::parseXML($xmlData);

Output: ['options' => ['option1' => 'value1']]

Main functionalities:

  • Check if an XML string is valid
  • Convert data to appropriate types
  • Decode a SimpleXMLElement object into an array or a string
  • Parse XML data into an array

Methods:

  • isValidXML($xml): Checks if an XML string is valid by loading it as a SimpleXMLElement object and using XMLReader to validate it.
  • convertData($data): Converts data to appropriate types, such as integers, floats, booleans, or leaves it as a string.
  • decodeXML($xml, $attributesKey, $reduce, $alwaysArray, $valueKeys): Decodes a SimpleXMLElement object into an array or a string. It extracts attributes, values, and children elements recursively.
  • extractAttributes($xml, $attributesKey, &$arr): Extracts attributes from a SimpleXMLElement object and adds them to an array.
  • extractValue($xml, &$arr, $valueKeys): Extracts the value from a SimpleXMLElement object and adds it to an array.
  • processChildren($xml, &$arr, $attributesKey, $reduce, $alwaysArray, $valueKeys): Processes the children of a SimpleXMLElement object and adds them to an array.
  • parseXML($xml): Parses XML data into an array. It uses decodeXML to decode the XML and extract options as a separate array.

Methods

static bool
isValidXML(mixed $xml)

Check if xml string is valid

static string|array
parseXML(string $xml)

Parse XML data and convert it into an array.

Details

static bool isValidXML(mixed $xml)

Check if xml string is valid

Parameters

mixed $xml

Argument to be tested

Return Value

bool

static string|array parseXML(string $xml)

Parse XML data and convert it into an array.

Parameters

string $xml

The XML data to parse.

Return Value

string|array

The parsed XML data as an array or a string.