ID:               19862
 Updated by:       [EMAIL PROTECTED]
 Reported By:      [EMAIL PROTECTED]
-Status:           Open
+Status:           Bogus
 Bug Type:         Documentation problem
 Operating System: any
 PHP Version:      4.2.3
 New Comment:

see http://php.net/xml_parse :

[...]
data

    Chunk of data to parse. A document may be parsed piece-wise by
calling xml_parse() several times with new data, as long as the
is_final parameter is set and TRUE when the last data is parsed. 

is_final (optional)

    If set and TRUE, data is the last piece of data sent in this parse.

[...]

it is totaly valid to split data chunks wherever you want, 
you may even feed xml_parse() character by character.

the manual example will not only work for local files but also for e.g.
http: remote streams where you can't get the input size in advance, and
for any kind of non-blocking stream where fread() may return less than
the requested number of bytes even if EOF is not yet reached
 


Previous Comments:
------------------------------------------------------------------------

[2002-10-11 08:54:43] [EMAIL PROTECTED]

Expat is a SAX parser. It can handle blocks (or streams as they're
preferred to be called these days) and doesn't care whether a document
is valid or even when it is a document.

The point of using a stream-based parser, is that you're not required
to open up files entirely (in fact - your input can be a socket).

Allthough the manual example dies, as soon as a parser error is
detected, one can actually retrieve the linenumber of the error, store
everything from that line on in a buffer, process the lines before that
and retrieve the next block, appending it to the buffer.

So I agree, the example is not very useful in some cases, but not for
your reasoning.

------------------------------------------------------------------------

[2002-10-11 08:01:36] [EMAIL PROTECTED]

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 this bug report at http://bugs.php.net/?id=19862&edit=1


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

Reply via email to