XML数据过早结束

时间:2013-10-14 02:21:01

标签: php xml wordpress

运行下面的代码时出现以下错误:

在标记请求行2

中加载XML过早结束失败

致命错误:在第61行的/home4/viptrav3/public_html/wp-content/themes/voyage-child/transfer.php中的非对象上调用成员函数asXml()

第61行是$requestFile->asXml('trequest.xml');

但我没有看到过早关闭XML。我想将其保存为XML文件trequest.xml

//Build XML Request
                $requestData = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
                $requestData .= '<Request>';

                // Create Request Header
                $requestData .= '<Source>';

                // Add Requestor ID data
                $requestData .= '<RequestorID Client="' . $clientID . '" EMailAddress="' . $email . '" Password="' . $password . '" />';

                // Add Requestor Preferences data
                $requestData .= '<RequestorPreferences Language="' . $language . '" Currency="USD" Country="US" >';
                $requestData .= '<RequestMode>' . $requestMode . '</RequestMode>';
                $requestData .= '</RequestorPreferences>';
                $requestData .= '</Source>';

                // Create Request Body
                $requestData .= '<RequestDetails>
                                <SearchTransferPriceRequest>
                                <TransferPickUp>
                                <PickUpCityCode>LON</PickUpCityCode>
                                <PickUpCode>A</PickUpCode>
                                <PickUpPointCode>LCY</PickUpPointCode>
                                </TransferPickUp>
                                <TransferDropOff>
                                <DropOffCityCode>EDI</DropOffCityCode>
                                <DropOffCode>H</DropOffCode>
                                </TransferDropOff>
                                <TransferDate>
                                2013-11-11</TransferDate>
                                <NumberOfPassengers>2</NumberOfPassengers>
                                <PreferredLanguage>E</PreferredLanguage>
                                </SearchTransferPriceRequest>
                                </RequestDetails>';                    
//Save Request & Display XML errors

                libxml_use_internal_errors(true);
                $sxe = simplexml_load_string($requestData);
                if ($sxe === false) {
                    echo "Failed loading XML\n";
                    foreach(libxml_get_errors() as $error) {
                        echo "\t", $error->message;
                    }
                }


                $requestFile = simplexml_load_string($requestData);
                $requestFile->asXml('trequest.xml');

1 个答案:

答案 0 :(得分:2)

您必须在结尾处关闭<Request>标记。

手动构建XML也不好。请改用DOMDocument课程。