douwulu2576 2016-06-01 14:13
浏览 32

如果给出了错误的URL,则处理SimpleXMLElement错误

I am getting some products via some xml files daily.

Sometimes there are 4 files, sometimes there are up to 10.

I want to process them in a loop with SimpleXMLElement from an URL.

Here is what I try:

for ($i = 1; $i <= 10; $i++) {
    try {
        $SimpleXML = new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
    } catch (Exception $e) {
        var_dump($e);
    }
}

But unluckily I've got warning:

SimpleXMLElement::__construct(): http://example.com/file_6.xml:42: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xE1 0x6D 0x6F 0x67

In this case, there were only 5 files, so when I try to get the 6th, that gaves me the main page from the site. I've also tried with to surpress the warning without success.

 $SimpleXML = @new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);

Is there any way to handle these errors in order to avoid them stopping my script?

EDIT

I can not use file_get_contents because in this case, I am getting memory limit error, and I can not increase the memory. These are so big files, one of it is 1GB. Guess what, the developer of these files are put the product images binaries into the file :((((((((( (I can not speak with him).

EDIT2

In php documentation (here) I've read $var = @new some_class(); should work.

If I am using:

$SimpleXML = @new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);

I get this:

Exception: String could not be parsed as XML in ........\Parser.php on line 19

I thought, yeah, it is much more better, because this is an Expection. If I wrap with try/catch, I just get the same error.

try {
    $SimpleXML = @new \MFF_System\SimpleXMLExtended($file, LIBXML_NOCDATA, true);
} catch (Exception $e) {
    die('Exception');
}

\MFF_System\SimpleXMLExtended is just extends the SimpleXMLElement class, and use a method to add CDATA, nothing special.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题