note 45372 deleted from function.xml-parse-into-struct by cmb
| From: | cmb@php.net | Date: | Sun, 06 Oct 2019 08:25:39 +0000 |
| Subject: | note 45372 deleted from function.xml-parse-into-struct by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212520@lists.php.net to get a copy of this message | ||
Note Submitter: rob at digital hyphen crocus dot com
----
I wrote this function in a bad mood, to get some data you can loop through. sprint_r or var_dump its
output to see what it does. It's not pretty, but at least you can loop through the data without
a LOT of hassle.
This can also be done with handlers, but I couldn't be bothered with global variables.
<?php
$data = xml_parse_into_struct($somexml);
function xml_getmeaningfuldata($data) {
foreach($data as $element) {
if ($element['type'] == "open") {
if (!$elementnos[$parents.$element['tag']]) {
$elementnos[$parents.$element['tag']] = 1;
}
$elements[$parents.$element['tag'].":".
$elementnos[$parents.$element['tag']]]['attributes'] =
$element['attributes'];
$elements[$parents.$element['tag'].":".
$elementnos[$parents.$element['tag']]]['value'] =
trim($element['value']);
$parents .=
$element['tag'].":".$elementnos[$parents.$element['tag']]."^";
}
if ($element['type'] == "cdata") {
if (trim($element['value'])) {
$elements[$parents.$element['tag'].":".
$elementnos[$parents.$element['tag']]]['values'] = $element['value'];
}
}
if ($element['type'] == "complete") {
if (!$elementnos[$parents.$element['tag']]) {
$elementnos[$parents.$element['tag']] = 1;
}
$elements[$parents.$element['tag'].":".
$elementnos[$parents.$element['tag']]]['attributes'] =
$element['attributes'];
$elements[$parents.$element['tag'].":".
$elementnos[$parents.$element['tag']]]['value'] =
trim($element['value']);
$elementnos[$parents.$element['tag']]++;
}
if ($element['type'] == "close") {
$parents = substr($parents, 0, strrpos(substr($parents, 0, -1), "^")+1);
$elementnos[$parents.$element['tag']]++;
// if (sizeof($parents) == 1) unset($parents);
}
}
return $elements;
}