Here is another example of detecting empty elements. I works with libxml2. Note that it handles buffer boundaries.
<?php
$depth = 0; $empty = false; $offset = 0; function tagStart($parser, $name, array $attribs) {
global $depth, $empty, $data, $offset, $lastchar;
$idx = xml_get_current_byte_index($parser);
if (isset($data[$idx - $offset])) {
$c = $data[$idx - $offset];
} else {
$c = $lastchar;
}
$empty = $c == '/';
echo str_repeat("\t", $depth), "<$name", ($empty ? '/>' : '>'), "\n";
if (!$empty) ++$depth;
}
function tagEnd($parser, $name) {
global $depth, $empty;
if (!$empty) {
--$depth;
echo str_repeat("\t", $depth), "</$name>\n";
} else {
$empty = false;
}
}
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, 'tagStart', 'tagEnd');
$data1 = '
<test>
<empty att="3" />
<nocontent></nocontent>
<content>
<empty/>
<empty/>
</content>
<empty/';
$data2 = '>
<empty att="5" />
</test>
';
$data = &$data1;
$length = strlen($data1);
$lastchar = $data[$length-1];
xml_parse($parser, $data1);
$offset .= $length;
$data = &$data2;
xml_parse($parser, $data2);