将 XML 字符串转换为对象时出错

问题描述 投票:0回答:1

这是我的代码:

    $response = Http::withHeaders([
            "Content-Type" => "application/xml",
            "User-Agent" => "Insomnia/2023.5.6",
            "Accept" => "application/xml"
        ])->withBody($xml, 'application/xml')->post($url);

        if (!$response->successful()) {
            Log::error($response);
            return 0;
        }

        $xmlObject = simplexml_load_string($response->body());
        $body = $xmlObject->children('SOAP-ENV', true)->Body;
        $response = $body->children('ns1', true)->StoreProductsResponse;
        $return = $response->return;
        $namespaces = $xmlObject->getNamespaces(true);

如果我执行 Log::info($response->body()),则会出现以下字符串:

<?xml version="1.0" encoding="UTF-8"?>
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://catalog.nexsysla.com/"><SOAP-ENV:Body><ns1:StoreProductsResponse><return><mark>3nStar</mark><parent>POS</parent><category>Cash Drawers</category><sku>CD250</sku><name>CD250</name><price>48</price><currency>USD</currency><tax_excluded>19</tax_excluded><inventory>4</inventory><short_description>3nStar CD250, Manual &amp; automatic cash drawer, Steel, Black, 330 mm, 343 mm, 90 mm</short_description><long_description>The Cash Drawer CD250 is the ideal choice for businesses needing a compact, heavy duty solution. Available as small as 330mm x 343mm x 90mm, the CD250 is small enough to be used in mobile payment stations such as kiosks or food vendor carts. The cash drawer uses a quick disconnect cable to interface with most major receipt printers, making installation a snap.</long_description>

这只是 xml 格式的产品信息示例。但是,问题是,当我使用 simplexml_load_string 将该字符串转换为 xml 后执行 Log::info( $xmlObject) 时,变量 $xmlObject 显示为空

并且没有出现任何错误或任何东西。只有变量显示为空

javascript php laravel
1个回答
0
投票

您的 XML 不完整。但假设是这样,你的代码读取它大部分是正确的。要验证

SimpleXMLElement
对象不为空,您可以使用
->asXML()
方法。

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="https://catalog.nexsysla.com/">
    <SOAP-ENV:Body>
        <ns1:StoreProductsResponse>
            <return>
                <mark>3nStar</mark>
                <parent>POS</parent>
                <category>Cash Drawers</category>
                <sku>CD250</sku>
                <name>CD250</name>
                <price>48</price>
                <currency>USD</currency>
                <tax_excluded>19</tax_excluded>
                <inventory>4</inventory>
                <short_description>3nStar CD250, Manual &amp; automatic cash drawer, Steel, Black, 330 mm, 343 mm, 90 mm</short_description>
                <long_description>The Cash Drawer CD250 is the ideal choice for businesses needing a compact, heavy duty solution. Available as small as 330mm x 343mm x 90mm, the CD250 is small enough to be used in mobile payment stations such as kiosks or food vendor carts. The cash drawer uses a quick disconnect cable to interface with most major receipt printers, making installation a snap.</long_description>
            </return>
        </ns1:StoreProductsResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
';

$xmlObject = simplexml_load_string($xml);
$body = $xmlObject->children('SOAP-ENV', true)->Body;
$response = $body->children('ns1', true)->StoreProductsResponse;
$return = $response->children('');
$properties = $return->children('');
foreach ($properties as $property) {
    echo $property->asXML() . "\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.