Voting

: five plus zero?
(Example: nine)

The Note You're Voting On

kok at nachon dot nl
16 years ago
Here is another example of detecting empty elements. I works with libxml2. Note that it handles buffer boundaries.

<?php

$depth
= 0; //current depth, used for pretty printing
$empty = false; //whether the tag is empty
$offset = 0; //the index of the start of the current buffer within the stream

function tagStart($parser, $name, array $attribs) {
global
$depth, $empty, $data, $offset, $lastchar;
$idx = xml_get_current_byte_index($parser);
/* xml_get_current_byte_index returns index within the streams and not
within the buffer.*/

/* Check if the index is within the buffer. */
if (isset($data[$idx - $offset])) {
$c = $data[$idx - $offset];
} else {
/* If it isn't simple use the last character of the buffer. */
$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);

<< Back to user notes page

To Top