PHP read XML giant Slow (big data volume)
I use PHP to read the merchant's product Api,xml format, the function of the Getxmldata () function, but because the data is too large, the runtime often times out. Is there any good way to read this big XML? Is there a part of the reading method? Specifically, the remaining points are given. Thank the Fairies first
Share to:
------Solution--------------------
Well, SimpleXML and DOMDocument are all loading XML into memory at once.
If the file is large, consider using XML parsing functions
For example in the Handbook
$file = "Data.xml";
$depth = Array ();
function startelement ($parser, $name, $attrs)
{
Global $depth;
for ($i = 0; $i < $depth [$parser]; $i + +) {
echo " ";
}
echo "$name \ n";
$depth [$parser]++;
}
function EndElement ($parser, $name)
{
Global $depth;
$depth [$parser]--;
}
$xml _parser = Xml_parser_create ();
Xml_set_element_handler ($xml _parser, "startelement", "endElement");
if (! ( $fp = fopen ($file, "R")) {
Die ("Could not open XML input");
}
while ($data = Fread ($fp, 4096)) {
if (!xml_parse ($xml _parser, $data, feof ($fp))) {
Die (sprintf ("XML error:%s @ line%d",
Xml_error_string (Xml_get_error_code ($xml _parser)),
Xml_get_current_line_number ($xml _parser)));
}
}
Xml_parser_free ($xml _parser);