Jump to content
php.lv forumi

Lielāka par 100 Mb XML faila transfērs uz MySQL


ffgghh

Recommended Posts

Šķiet ka jūs par mani esat īpaši augstās domās!!!

 

Bet vai šis ir pats ātrākais veidz kā iebāzt datus MySQL ???

 

require_once 'xml_saxy_parser.php';

$saxparser =& new SAXY_Parser();

$saxparser->xml_set_element_handler("startElementHandler", "endElementHandler");

$saxparser->xml_set_character_data_handler("characterHandler");

$npk="0";

$myXML = implode("",file("xml_y.xml"));

$success = $saxparser->parse($myXML);

if ($success) {

echo "\n<br /><br />Parsing successful!";

echo ShowArray($DATA, "array");

} else {

$errorCode = $saxparser->xml_get_error_code();

$errorString = $saxparser->xml_error_string($errorCode);

echo "\n<br /><br />Error Code: " . $errorCode;

echo "\n<br />Error String: " . $errorString;

}

function startElementHandler($parser, $elementName, $attrArray) {

GLOBAL $npk, $DATA, $DATA_NAME;

$DATA_NAME=$elementName;

if($elementName!="VFPData"){

if($elementName=="klients"){if(isset($npk)){$npk++;}else{$npk=1;}

}else{$DATA[$npk][$elementName]="";}

}

}

function endElementHandler($parser, $elementName) {}

function characterHandler($parser, $text) {

GLOBAL $npk, $DATA, $DATA_NAME;

if($DATA_NAME!="VFPDATA"&&$DATA_NAME!="KLIENTS"){

$DATA[$npk][$DATA_NAME]=$DATA[$npk][$DATA_NAME].$text;

}

}

 

 

STARPCITU - tas SAXy izpildās lēnāk nekā:

 

require_once 'XML/Parser.php';

class myParser extends XML_Parser

{

function myParser() {parent::XML_Parser();}

function startHandler($xp, $name, $attribs) {GLOBAL $npk, $DATA, $DATA_NAME;

$DATA_NAME=$name;

if($name!="VFPDATA"){

if($name=="KLIENTS"){if(isset($npk)){$npk++;}else{$npk=1;}

}else{$DATA[$npk][$name]="";}

}

}

function endHandler($xp, $name){}

function cdataHandler($xp, $cdata) {GLOBAL $npk, $DATA, $DATA_NAME;

if($DATA_NAME!="VFPDATA"&&$DATA_NAME!="KLIENTS"){

$DATA[$npk][$DATA_NAME]=$DATA[$npk][$DATA_NAME].$cdata;

}

}

}

/*############################################################################################################*/

$p = &new myParser();

$result = $p->setInputFile('xml_1.xml');

$result = $p->parse();

echo ShowArray($DATA, "array");

Link to comment
Share on other sites

Da tu ko... murgs..

$myXML = implode("",file("xml_y.xml"));

 

Kam tev izmantot visādas neznāmas klases...

Es takš tev teicu PHP manuālī ir piemērs, kā in-direct nopārsēt XML un apstrādāt to... blin, ku cilvēki mēdz būt ietiepīgi.

Link to comment
Share on other sites

<?php
$file = "mans.xml";
$record = null;

function startElement($xml_parser,  $attrs )
{
global $record;
if ($attrs=='RECORD')
{
	$record = null;
}
}

function endElement($xml_parser, $name)
{
global $info;
global $record;

if ($name == 'RECORD')
{
	print "DO_INSERT_".print_r($record,1)."<br />\n";
} else {
	$record->$name = $info;
}

$info = "";
}

function charData($xml_parser, $data )
{
global $info;
$info .= $data;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData");
$fp = fopen($file, "r");
while ($data = fread($fp, 1024))
{
xml_parse($xml_parser, $data, feof($fp));
}

xml_parser_free($xml_parser);

?>

 

<?xml version="1.0"?>
<table name="mana_tabula">
<record>
  <field1>Data 1</field1>
  <field2>Data 2</field2>
</record>
</table>

 

Un vispār, šadas lietas jādara ar kādu zemāku valodu.. piem uzrakstīt konsoles tūli XML_to_inserts.exe un tad iebarot jau gatavus insertus

Edited by Delfins
Link to comment
Share on other sites

×
×
  • Create New...