Php reading xml is very slow (large data volume). how can this problem be solved?

Source: Internet
Author: User
Php reading xml is very slow (large data volume). I use php to read the merchant's product API. in xml format, the function type getxmldata () is used, but because the data is too large, the running time often times out. Is there any good way to read this large xml? Is there a way to read part of the data? Specifically, all the remaining points are given. Thank you ~ Php reading xml is very slow (large data volume)
I use php to read the merchant's product API. in xml format, the function type getxmldata () is used. However, because the data is too large, the running time often times out. Is there any good way to read this large xml? Is there a way to read part of the data? Specifically, all the remaining points are given. Thank you ~ Share:
------ Solution --------------------
Well, SimpleXML and DOMDocument all load xml to the memory at a time.
If the file is large, you can use XML syntax to parse the function.
Example in the manual
$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 at 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.