From:             [EMAIL PROTECTED]
Operating system: win xp
PHP version:      4.2.3
PHP Bug Type:     Documentation problem
Bug description:  online documentation is bugged...

URL: http://www.php.net/manual/en/ref.xml.php

how the files are opened is not the correct way or the input/output may
get mixed up or truncated on same cases...

I mean ... you should not open files in blocks, but whole file ... I copy
pasted your example to my code and when some block (of those 4096 bytes)
end and next blocks start value somehow happened to be in the middle XML
value, then the value was truncated or cutted half in output. anyway the
input (cycle) should be in one piece (whole file at once) or row by row,
not by constant number of bytes.



Example 1. (your version)

$file = "data.xml";
$depth = array();

function startElement($parser, $name, $attrs) {
    global $depth;
    for ($i = 0; $i < $depth[$parser]; $i++) {
        print "  ";
    }
    print "$name\n";
    $depth[$parser]++;
}

function endElement($parser, $name) {
    global $depth;
    $depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf("XML error: %s at line %d",
                    xml_error_string(xml_get_error_code($xml_parser)),
                    xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);




BUT THIS SHOULD BE (my version):

Example 1.

$file = "data.xml";
$depth = array();

function startElement($parser, $name, $attrs) {
    global $depth;
    for ($i = 0; $i < $depth[$parser]; $i++) {
        print "  ";
    }
    print "$name\n";
    $depth[$parser]++;
}

function endElement($parser, $name) {
    global $depth;
    $depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
}

$data = fread($fp, filesize($file)));
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

xml_parser_free($xml_parser);

-- 
Edit bug report at http://bugs.php.net/?id=19862&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=19862&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=19862&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=19862&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=19862&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=19862&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=19862&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=19862&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=19862&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=19862&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=19862&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=19862&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=19862&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=19862&r=isapi


-- 
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to