Description
One of the biggest challenges that I've been dealing with in a project that I've been working on has involved two specific issues that I have yet to be able to solve, when converting XML fragments into arrays. The first is in dealing with "mixed" content, and the other is with preserving the XML fragment's tag order. The following XML code illustrates both problems quite well:
<category>
<template>
<random>
<li>Choice 1</li>
<li>Choice 2</li>
<li>Choice 3</li>
</random>
This is text.
<random>
<li>Pick A</li>
<li>Pick B</li>
<li>Pick C</li>
</random>
</template>
</category>
The first problem here when using your script is that the <TEMPLATE>
tag contains both plain text and child elements, and this throws the following error:
Fatal error: Cannot use string offset as an array in X:\GCC\tmp\XML_tests\xmlstr_to_array.php on line 39
Now I've implemented a 'work-around' for that part, by simply surrounding the plain text with <TEXT>
tags, but this leads me to the second problem that I'm seeing, namely that the 'tag order' is lost when the array is created, due to the presence of two distinct <RANDOM>
tags, one before the <TEXT>
tag, and one after. Your script (correctly) places each of these elements into an array, but in doing so, the sequence of <RANDOM><TEXT><RANDOM>
is lost, and as a result, makes the array useless to my needs. I've been searching for over a month to locate a solution to this problem, but so far, have come up empty.