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 我现在有一些关于提升机故障的专有文本数据,量也不多,我在label studio上进行了关系和实体的标注,完成了知识图谱的构造,那么我使用生成式模型的话,我能做哪些工作来写我的论文?
    • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
    • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
    • ¥15 在ubuntu中无法连接到远程服务器传输文件
    • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
    • ¥15 Oracle触发器字段变化时插入指定值
    • ¥15 docker无法进入容器内部
    • ¥15 qt https 依赖openssl 静态库
    • ¥15 python flask 报错
    • ¥15 改个密码引发的项目启动问题