PHP read XML giant Slow (big data volume), how to deal with

Source: Internet
Author: User
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);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.