douya1061 2013-06-20 09:11
浏览 23
已采纳

无法在PHP中捕获XML异常/错误

I am not able to catch exception with below code. Can anyone help me with this thing?

try
{
    $xml_emp_name = $xpath->evaluate("//EMPLOYEES[ID='" . $emp_id . "']/EMP-NAME/text()")->item(0)->nodeValue;
}
catch(Exception $e)
{
    echo "Error: " . $e->getMessage();
}
  • 写回答

3条回答 默认 最新

  • duanke9540 2013-06-20 12:55
    关注

    Your code is prone to xpath injection. Fix that first. The error then goes away automatically (because the xpath can not become syntactically invalid). Also you need to check/validate return values.

    So you're missing the basic principles of input validation and return value validation. All you need to do is to take more care.

    Input validation:

    You directly inject the variable $emp_id into the xpath string for substitution:

    "//EMPLOYEES[ID='" . $emp_id . "']/EMP-NAME/text()"
    

    However at that place you can not have a single quote inside that string. Instead check the input value (Validation) or filter/streamline it (Sanitization). For exampe, validate that it does not contain a single quote or sanitize for a numeric value. Here the second:

    $expression = sprintf('//EMPLOYEES[ID="%d"]/EMP-NAME/text()', $emp_id);
    $result     = $xpath->evaluate($expression);
    

    This little call to sprintf() takes care that only numeric integer values are being used. They never contain quotes, so the expression is always valid. Invalid values that are no number will become 0. As it's the general principle to never assign the ID 0 this should normally not cause any issue in a well designed system. If you want to do the filtering more granular please see Data Filtering in the PHP manual.

    return value validation

    In your code you just take over the return value of the result with very little checks (actually no checks). That is wrong. For each method or function you use you need to look it up in the PHP manual and check the documentation for all possible return values. Here the method is DOMXpath::evaluate(), click the link and locate the Return Values section. You find this for each method and function in the PHP manual.

    When you read the documentation also figure out which kind of error-handling a method makes use of. Does it throws exceptions (and if yes, which ones?) or does it show an error-condition with it's return value (like in your case)? This information is needed to decide whether to do try/catch as you did (and which is wrong because it does not throw exceptions) or if you need to check the return value:

    $expression = sprintf('//EMPLOYEES[ID="%d"]/EMP-NAME/text()', $emp_id);
    $result     =  $xpath->evaluate($expression);
    
    if (!$result) {
        throw new Exception(
            sprintf('No such employee (id: %s)', var_export($emp_id, true))
        );
    }
    

    This example turns a falsy return value into an exception with an individual exception message. You also might want to consider a different exception, the SPL offers some pre-defined exceptions.

    I hope this answer helps you to deal with this issue and forthcoming ones.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?