Convert XML to Array
Posted: Wed Nov 08, 2006 10:18 am
Does anybody know how to convert XML to an array? (Actually XHTML)
Currently I am recursively casting to array from a simpleXml object but this doesn't appear to work correctly all the time.
Example:note the 'note' and 'error' classes on the list items
I'm doing this because I want to test for equality between to pieces of HTML. The HTML is considered equal when they contain the same tags in the same structure with the same attributes with the same values. Critically, neither whitespace between tags nor attribute order should not be considered grounds for inequality.
This is the full code of what I'm doing at the moment:
Currently I am recursively casting to array from a simpleXml object but this doesn't appear to work correctly all the time.
Example:
Code: Select all
<div><ul><li class="error">Zim</li><li><ul><li class="error">Foo</li><li class="error">Bar</li></ul></li><li class="note">Gir</li></ul><label for="txt">foo</label><textarea disabled="disabled" id="txt" name="txt" rows="5" cols="20"></textarea></div>Code: Select all
Array
(
[div] => Array
(
[label] => foo
[textarea] => Array
(
[@attributes] => Array
(
[cols] => 20
[disabled] => disabled
[id] => txt
[name] => txt
[rows] => 5
)
)
[ul] => Array
(
[li] => Array
(
[0] => Zim
[1] => Array
(
[ul] => Array
(
[li] => Array
(
[0] => Foo
[1] => Bar
)
)
)
[2] => Gir
)
)
)
)This is the full code of what I'm doing at the moment:
Code: Select all
protected function assertEqualHtml($actual, $expected)
{
$actualXml = simplexml_load_string('<root>' . $actual . '</root>');
$expectedXml = simplexml_load_string('<root>' . $expected . '</root>');
$actual = (array)$actualXml;
$expected = (array)$expectedXml;
$this->_ksortRecursive($actual);
$this->_ksortRecursive($expected);
if ($actual !== $expected) {
$this->fail('HTML Equality assertion');
echo '<br /><b>This...</b>';
$this->dump($actual);
echo '<b>should be equal to this...</b>';
$this->dump($expected);
return false;
}
echo '<br /><b>These are considered equal...</b>';
$this->dump($actual);
$this->dump($expected);
$this->pass();
return true;
}
private function _ksortRecursive(array &$array)
{
ksort($array);
foreach ($array as &$v) {
if (is_object($v)) { // recursive array cast here
$v = (array)$v;
}
if (is_array($v)) {
$this->_ksortRecursive($v);
}
}
}