| | |
XML parser routine: broken?
![]() |
Hello everyone. I'm working on getting threads of replies to work in my forum system. I'm using this function:
to parse an XML file with fields like this:
So, the read_fields() sub is passed the number and names of fields to read and an offset telling it where to start searching in the file. This works in most cases, but in some situations it gets the <date+time> field wrong, while getting all others right. It either reads the field from the wrong record or comes back blank. I've started planning a work around to deal with this, but I'm troubled that I can't see where the bug is in there (and a patch is never the best solution). Can anyone see a gaping hole in my logic here? Any advice appriciated.
Steven.
perl Syntax (Toggle Plain Text)
sub read_fields { my($offset1, $offset2, $offset3, $offset4, $this_offset, $result, $n, $dbase, $data, @field_name, @name_length, $num_fields, $m, $check, $check2, $primary_key, $action, @passed_array); ($offset1, $offset2, $result, $dbase, $num_fields, $m, $action, $primary_key, @passed_array) = @_; @field_name = splice(@passed_array, 0, 6); @name_length = splice(@passed_array, 0); # $action is always 1 in the cases where things go wrong. unless ($action == 1) { $field_name[0] = $field_name[5]; $name_length[0] = $name_length[5]; } for ($n = 0; $n < $num_fields; $n++) { $field_name[$n + 6] = "</" . substr($field_name[$n], 1); $offset3 = index($$dbase, $field_name[$n], $offset1) + $name_length[$n]; $offset4 = index($$dbase, $field_name[$n + 6], $offset3) - 1; unless ($n != 4) {$this_offset = $offset3} @$result[$n] = substr($$dbase, $offset3, $offset4 - $offset3 + 1); } return $this_offset; }
to parse an XML file with fields like this:
Perl Syntax (Toggle Plain Text)
<record> <links>#either some text or just spaces</links> <post_id>000000048</post_id> <subject>#some text</subject> <author>#some text</author> <data+time>#some text</date+time> <content>#some text</content> </record>
So, the read_fields() sub is passed the number and names of fields to read and an offset telling it where to start searching in the file. This works in most cases, but in some situations it gets the <date+time> field wrong, while getting all others right. It either reads the field from the wrong record or comes back blank. I've started planning a work around to deal with this, but I'm troubled that I can't see where the bug is in there (and a patch is never the best solution). Can anyone see a gaping hole in my logic here? Any advice appriciated.
Steven.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
![]() |
Similar Threads
- xml parser to validate xml files-Urgent (XML, XSLT and XPATH)
- multi dimensional array search xml parser (PHP)
- How to re-organized XML file and call XML file from my software (RSS, Web Services and SOAP)
- saving xml file from servlet request instance (Java)
- Reading stream with two xml files in (Java)
- What is Parser? (Java)
- XHTML Complient parser? (HTML and CSS)
Other Threads in the Perl Forum
- Previous Thread: Search and Replace Script
- Next Thread: Help
| Thread Tools | Search this Thread |





