. ======================================================================*/ function kjwresultset_from_raw_xml($raw_xml, $row_tag = 'row') { require_once(dirname(__FILE__) . '/KjwArrayResultSet.php'); $ret = array(); $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $raw_xml, $values, $tags); xml_parser_free($parser); foreach ($tags as $key => $val) { if ($key == $row_tag) { for ($i = 0; $i < count($val); $i += 2) { $off = $val[$i] + 1; $arr = array_slice($values, $off, $val[$i + 1] - $off); $tmp = array(); foreach ($arr as $key2 => $val2) { $tmp[$val2['tag']] = $val2['value']; } array_push($ret, $tmp); } } else { continue; } } return new KjwArrayResultSet($ret); } function kjwresultset_to_raw_xml(&$result, $row_tag = 'row', $root_tag = 'ROOT') { $at = $result->at(); $result->seekAbs(0); $ret = array(); array_push($ret, '<' . $root_tag . '>'); while (($row = $result->getNext())) { array_push($ret, ' <' . $row_tag . '>'); foreach ($row as $key => $val) { array_push($ret, ' <' . $key . '>' . htmlentities($val, ENT_COMPAT, 'UTF-8') . ''); } array_push($ret, ' '); } array_push($ret, ''); $result->seekAbs($at); return implode("\n", $ret); } function kjwresultset_from_csv($csv_without_head, $delimiter = ',') { require_once(dirname(__FILE__) . '/KjwArrayResultSet.php'); assert(strlen($delimiter) == 1); $pos = 0; $delim_eol = "\\$delimiter\r\n"; $re = '!("(""|[^"])*"|[^"'.$delim_eol.']*)(['.$delim_eol.']|$)!'; $rows = array(); $columns = array(); while (1) { if (preg_match($re, $csv_without_head, $m, PREG_OFFSET_CAPTURE, $pos)) { if ($m[2][1] != -1) array_push($columns, str_replace('""', '"', substr($m[1][0], 1, -1))); else array_push($columns, $m[1][0]); if ($m[3][0] != $delimiter) { array_push($rows, $columns); $columns = array(); } if ($m[3][0] == '') break; $pos = $m[3][1] + 1; if ($m[3][0] == "\r") { // either \r, \r\n or \n if (substr($csv_without_head, $pos, 1) == "\n") ++$pos; } } } return new KjwArrayResultSet($rows); } function kjwresultset_from_csv_with_head($csv, $delimiter = ',') { // either \r, \r\n or \n if (!preg_match("![\r\n]!", $csv, $m, PREG_OFFSET_CAPTURE, 0)) return kjwresultset_from_csv('', $delimiter); $pos = $m[0][1] + 1; if ($m[0][0] == "\r") { if (substr($csv, $pos, 1) == "\n") ++$pos; } return kjwresultset_from_csv(substr($csv, $pos), $delimiter); } function kjwresultset_to_csv(&$result, $delimiter = ',', $with_head = false) { $at = $result->at(); $result->seekAbs(0); $ret = array(); // Get first row and use it as header if (($row = $result->getNext()) === false) return ''; if ($with_head) { $columns = array(); foreach (array_keys($row) as $value) { if (strpos($value, $delimiter) === false && strpos($value, '"') === false) array_push($columns, $value); else array_push($columns, '"' . str_replace('"', '""', $value) . '"'); } array_push($ret, $columns); } // Print all rows do { $columns = array(); foreach ($row as $value) { if (strpos($value, $delimiter) === false && strpos($value, '"') === false) array_push($columns, $value); else array_push($columns, '"' . str_replace('"', '""', $value) . '"'); } array_push($ret, $columns); } while (($row = $result->getNext())); // Reset result and return csv $result->seekAbs($at); return implode("\n", $ret); } function kjwresultset_to_csv_with_head(&$result, $delimiter = ',') { return kjwresultset_to_csv($result, $delimiter, true); } ?>