duancuisan2503 2017-03-28 11:07
浏览 95
已采纳

加快使用PHP中的DOMDocument类和命名空间解析XML文档

I have 6 XML documents that I need to parse with PHP. Every file has 50000 elements therefore I need fast parser so I chose DOMDocument class. Example of XML file is:

<?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.

What can I do to improve it?

  • 写回答

1条回答 默认 最新

  • duanlipeng4136 2017-06-15 01:45
    关注

    Xpath queries are much faster than doing normal traversal using 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>';
            }
    
        }
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵