【问题标题】:Parsing XML in PHP with SimpleXML使用 SimpleXML 在 PHP 中解析 XML
【发布时间】:2015-06-17 09:06:34
【问题描述】:

我正在尝试解析以下 XML,我尝试了很多不同的解决方案,我提供了一个我尝试过的示例。我已经阅读了 SimpleXML 文档,但我仍然无法正确理解。在下面的示例中,我要做的就是在 XML 中回显一行。

<?php
$xmlstr = '
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SubmitLeadResponse xmlns="https://test.com/">
<SubmitLeadResult>
<Result>C</Result>
<RedirectURL>https://testred.com</RedirectURL>
<ApplicantID>123</ApplicantID>
<ConfirmedPrice>0</ConfirmedPrice>
<PotentialPrice>0</PotentialPrice>
</SubmitLeadResult>
</SubmitLeadResponse>
</soap:Body>
</soap:Envelope>'
;

?>


<?php

$SubmitLeadResponse = new SimpleXMLElement($xmlstr);
echo $SubmitLeadResponse->SubmitLeadResult[0]->RedirectURL;

?>

【问题讨论】:

    标签: php xml parsing


    【解决方案1】:

    您可以尝试下面的 SimpleXML 代码

    <?php
    $xml ='<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <SubmitLeadResponse xmlns="https://test.com/">
    <SubmitLeadResult>
    <Result>C</Result>
    <RedirectURL>https://testred.com</RedirectURL>
    <ApplicantID>123</ApplicantID>
    <ConfirmedPrice>0</ConfirmedPrice>
    <PotentialPrice>0</PotentialPrice>
    </SubmitLeadResult>
    </SubmitLeadResponse>
    </soap:Body>
    </soap:Envelope>';
    $get_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $xml);
    $xml = simplexml_load_string($get_xml);
    print"<pre>";
    print_r((string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->RedirectURL);
    echo "<br /><br /><br />";
    print_r($xml);
    ?>
    

    【讨论】:

    • 嗨@Chauhan,谢谢。你能添加你的例子吗,如果 RedirectURL 包含一个 URL,它会被重定向到 www.test.com,如果没有,则回显“失败”
    • $getRedirectUrl = (string)$xml->Body->SubmitLeadResponse->SubmitLeadResult->RedirectURL; if($getRedirectUrl) { header("位置:".$getRedirectUrl); } else { 回声“失败”;在 $xml = simplexml_load_string($get_xml); 之后添加这一行
    • 非常感谢,如果我能问你最后一个问题,我保证。我该怎么做:如果结果 = C 然后使用重定向 URL 进行重定向,如果结果是任何其他值回显失败。非常感谢:D
    • 我没有得到你的 Result = C?
    • 在 XML 中我得到一个 C 。如果我得到一个 C 那么他们提供一个 URL,如果我得到任何其他结果那么他们提供一个失败的 url
    【解决方案2】:

    我稍微更改了您的代码。这是获取 RedirectURL 的工作示例:

    <?php
    $xmlstr = '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <SubmitLeadResponse xmlns="https://test.com/">
    <SubmitLeadResult>
    <Result>C</Result>
    <RedirectURL>https://testred.com</RedirectURL>
    <ApplicantID>123</ApplicantID>
    <ConfirmedPrice>0</ConfirmedPrice>
    <PotentialPrice>0</PotentialPrice>
    </SubmitLeadResult>
    </SubmitLeadResponse>
    </soap:Body>
    </soap:Envelope>';
    
    $doc = new DOMDocument();
    $doc->loadXML( $xmlstr );
    
    $RedirectURL = $doc->getElementsByTagName( "RedirectURL" );
    $RedirectURL = $LoginResults->item(0)->nodeValue;
    
    var_dump( $RedirectURL );
    

    此来源提供的示例:There are also more informations to reading SOAP-envelopes without a soapclient

    请注意,最好省略 php 结束标记并尽可能长时间地留在 php-context 中以避免意外输出(换行符)。

    【讨论】:

      【解决方案3】:

      您的 XML 包含命名空间元素,因此解析起来稍微复杂一些。可以通过将命名空间值传递给children() 来完成,如下所示:

      Codepad demo

      $SubmitLeadResponse = new SimpleXMLElement($xmlstr);
      echo (string)$SubmitLeadResponse
                ->children('http://schemas.xmlsoap.org/soap/envelope/')
                ->Body
                ->children('https://test.com/')
                ->SubmitLeadResponse
                ->SubmitLeadResult
                ->RedirectURL;
      

      输出

      https://testred.com
      

      注意:SimpleXML 不喜欢 XML 字符串前的新行,因此删除新行,使其变为:

      $xmlstr = '<?xml version="1.0" encoding="utf-8"?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多