XML parser routine: broken?

Reply

Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

XML parser routine: broken?

 
0
  #1
Jan 16th, 2007
Hello everyone. I'm working on getting threads of replies to work in my forum system. I'm using this function:

  1. sub read_fields {
  2. 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);
  3. ($offset1, $offset2, $result, $dbase, $num_fields, $m, $action, $primary_key, @passed_array) = @_;
  4. @field_name = splice(@passed_array, 0, 6); @name_length = splice(@passed_array, 0);
  5. # $action is always 1 in the cases where things go wrong.
  6. unless ($action == 1) {
  7. $field_name[0] = $field_name[5];
  8. $name_length[0] = $name_length[5];
  9. }
  10. for ($n = 0; $n < $num_fields; $n++)
  11. {
  12. $field_name[$n + 6] = "</" . substr($field_name[$n], 1);
  13. $offset3 = index($$dbase, $field_name[$n], $offset1) + $name_length[$n];
  14. $offset4 = index($$dbase, $field_name[$n + 6], $offset3) - 1;
  15. unless ($n != 4) {$this_offset = $offset3}
  16. @$result[$n] = substr($$dbase, $offset3, $offset4 - $offset3 + 1);
  17. }
  18. return $this_offset;
  19.  
  20. }

to parse an XML file with fields like this:
  1. <record>
  2. <links>#either some text or just spaces</links>
  3. <post_id>000000048</post_id>
  4. <subject>#some text</subject>
  5. <author>#some text</author>
  6. <data+time>#some text</date+time>
  7. <content>#some text</content>
  8. </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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC