我在SO中搜索过,但我发现的任何内容都无法帮助我。
我正在与货运服务JadLog进行系统集成。
当我使用产品和交付变量传递查询时,将返回XML。
返回示例:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<valorarResponse xmlns="">
<ns1:valorarReturn xmlns:ns1="http://jadlogEdiws">
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.jadlog.com.br/JadlogEdiWs/services">
<Jadlog_Valor_Frete>
<versao>1.0</versao>
<Retorno>1458,62</Retorno>
<Mensagem>Valor do Frete</Mensagem>
</Jadlog_Valor_Frete>
</string>
</ns1:valorarReturn>
</valorarResponse>
</soapenv:Body>
</soapenv:Envelope>
所以,有多个XML声明,对吧?
我需要的唯一价值是运费的价值,它位于Retorno标签中。我是这个案例,1458,62。
我想做什么:
$your_xml_response = file_get_contents($url_project);
$clean_xml = str_replace('soapenv:', '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);
var_dump($xml);
它的回报:
object(SimpleXMLElement)[1]
public 'Body' =>
object(SimpleXMLElement)[2]
public 'valorarResponse' =>
object(SimpleXMLElement)[3]
如果我尝试回显$ xml-&gt; Retorno,则返回空。
如何获取标签Retorno的值?
答案 0 :(得分:0)
当我尝试使用simplexml_load_string
加载XML字符串时,它会导致我
xml中不允许有多个XML声明。这就是我使用常规快递获得 Retorno 价值的原因。可能它不是在XML上使用正则表达式的最佳方法,但你可以尝试一下。
<?php
$re = '/<Retorno>(.*?)<\/Retorno>/m';
$str = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<valorarResponse xmlns="">
<ns1:valorarReturn xmlns:ns1="http://jadlogEdiws">
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.jadlog.com.br/JadlogEdiWs/services">
<Jadlog_Valor_Frete>
<versao>1.0</versao>
<Retorno>1458,62</Retorno>
<Mensagem>Valor do Frete</Mensagem>
</Jadlog_Valor_Frete>
</string>
</ns1:valorarReturn>
</valorarResponse>
</soapenv:Body>
</soapenv:Envelope>
';
preg_match_all($re, $str, $matches);
//debugging
print '<pre>';
print_r($matches);
print '<pre>';
//result
echo $matches[0][0];