Parser begrænsning
Er der nogen, som har et kvalificeret bud på, hvorfor følgende script absolut kun vil parse, 21 ud af ca. 90 poster (post1 - post90)?##
<?php
$GLOBALS['data'] = array();
$GLOBALS['current'] =& $GLOBALS['data'];
function startTag($parser, $name, $attribs) {
$GLOBALS['current'][$name]['parent'] =& $GLOBALS['current'];
$GLOBALS['current'] =& $GLOBALS['current'][$name];
}
function endTag($handler, $name) {
$temp =& $GLOBALS['current'];
$GLOBALS['current'] =& $temp['parent'];
unset($temp['parent']);
}
function data($handler, $data) {
$data = trim($data);
if(!empty($data)) {
$GLOBALS['current'][] = $data;
}
}
$parser = xml_parser_create('') or die('Failed to create parser');
xml_set_element_handler($parser, 'startTag', 'endTag') or die ("Failed to set element handler");
xml_set_character_data_handler($parser, 'data') or die ("Failed to set data handler");
$fp = fopen('data.xml', 'r');
while ($xml = fread($fp, 4096))
xml_parse($parser, $xml, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($parser);
$data = $GLOBALS['data']['D0']['D1'];
foreach($data as $name => $value) {
foreach($value as $index => $text) {
$tempArr[$index][$name] = $text;
}
}
$db = mysql_connect('xxx', 'yyy', 'zzz');
mysql_select_db('xxx', $db);
$dbfields = "D0, D1, " . implode(', ', array_keys($tempArr[0]));
foreach($tempArr as $value) {
$dbdata = implode("', '", $value);
$sql = "INSERT INTO table ($dbfields) VALUES('D0', 'D1', '$dbdata')";
mysql_query($sql, $db);
}
mysql_close($db);
?>
##
xml fil: data.xml
##
<?xml version='1.0' encoding='ISO-8859-1'?>
<D0>
<D1>
<D2>post1</D2>
<D3>post1</D3>
<D4>post1</D4>
<D5>data1</D5>
<D6>data1</D6>
<D7>data1</D7>
<D8>data1</D8>
<D9>data1</D9>
<D10>data1</D10>
</D1>
<D1>
<D2>post2</D2>
<D3>post2</D3>
<D4>post2</D4>
<D5>post2</D5>
<D6>post2</D6>
<D7>post2</D7>
<D8>post2</D8>
<D9>post2</D9>
<D10>post2</D10>
</D1>
<D1>
<D2>post3</D2>
<D3>post3</D3>
<D4>post3</D4>
<D5>post3</D5>
<D6>post3</D6>
<D7>post3</D7>
<D8>post3</D8>
<D9>post3</D9>
<D10>post3</D10>
</D1>
</D0>
##
