[英]Speed up parsing XML documents with DOMDocument class in PHP and with namespaces
I have 6 XML documents that I need to parse with PHP. 我有6个需要使用PHP解析的XML文档。 Every file has 50000 elements therefore I need fast parser so I chose DOMDocument class. 每个文件有50000个元素,因此我需要快速解析器,所以我选择了DOMDocument类。 Example of XML file is: XML文件的示例是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:PinsCountryCodeIds xmlns:ns2="http://apis-it.hr/umu/2015/types/kp">
<ns2:PinCountryCodeId>
<ns2:CountryCodeId>HR</ns2:CountryCodeId>
<ns2:PinPrimatelja>000000000</ns2:PinPrimatelja>
</ns2:PinCountryCodeId>
<ns2:PinCountryCodeId>
<ns2:CountryCodeId>HR</ns2:CountryCodeId>
<ns2:PinPrimatelja>000000001</ns2:PinPrimatelja>
</ns2:PinCountryCodeId>
<ns2:PinCountryCodeId>
<ns2:CountryCodeId>HR</ns2:CountryCodeId>
<ns2:PinPrimatelja>000000002</ns2:PinPrimatelja>
</ns2:PinCountryCodeId>
</ns2:PinsCountryCodeIds>
The best what I come up with is this code: 我想到的最好的是此代码:
$input_file=scandir($OIB_path);//Scanning directory for files
foreach ($input_file as $input_name){
if($input_name=="." || $input_name=="..")
continue;
$OIB_file=$OIB_path . $input_name;
$doc = new DOMDocument();
$doc->load( $OIB_file );
$doc->saveXML();
foreach ($doc->getElementsByTagNameNS('http://apis-it.hr/umu/2015/types/kp', 'PinPrimatelja') as $element) {
echo $element->nodeValue, ', <br> ';
}
}
But it is too slow it takes more then 20 minutes to parse 6 files. 但是它太慢了,解析6个文件要花费超过20分钟的时间。
What can I do to improve it? 我该怎么做才能改善它?
Xpath queries are much faster than doing normal traversal using DOM. Xpath查询比使用DOM进行普通遍历要快得多。
Try below code and let me know if it improves the performance. 尝试下面的代码,让我知道它是否可以提高性能。
<?php
$input_file=scandir($OIB_path);//Scanning directory for files
foreach ($input_file as $input_name){
if($input_name=="." || $input_name=="..")
continue;
$OIB_file=$OIB_path . $input_name;
$doc = new DOMDocument();
$doc->load( $OIB_file );
$xpath = new DOMXPath($doc);
$xpath->registerNameSpace('x', 'http://apis-it.hr/umu/2015/types/kp');
$elements = $xpath->query('//x:PinCountryCodeId/x:PinPrimatelja');
if ($elements->length > 0) {
foreach ($elements as $element) {
echo $element->nodeValue.'<br>';
}
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.