XML
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 aSimpleXMLElementobject and usingXMLReaderto 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 aSimpleXMLElementobject into an array or a string. It extracts attributes, values, and children elements recursively.extractAttributes($xml, $attributesKey, &$arr): Extracts attributes from aSimpleXMLElementobject and adds them to an array.extractValue($xml, &$arr, $valueKeys): Extracts the value from aSimpleXMLElementobject and adds it to an array.processChildren($xml, &$arr, $attributesKey, $reduce, $alwaysArray, $valueKeys): Processes the children of aSimpleXMLElementobject 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
static string|array
parseXML(string $xml)
Parse XML data and convert it into an array.