php调取webservice时解析xml遇到'<'和'>'的解决办法

本文介绍了一种解决XML文件中因包含特殊字符'&lt;'而导致解析失败的问题的方法。通过使用PHP的正则表达式及回调函数,可以有效地将这些特殊字符替换,从而确保XML文件能被正确解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做一个小项目时、由于是要调取一些医院系统的数据,而这些只提供webservice的xml数据。

程序调试到最后一天要演示的时候发现在xml里面多了一个'<'导致xml无法解析。找了很多办法!

1.通过stripos()函数定位到包含的xml标签的起始位置。

2.通过substr()函数截取标签

3.通过str_replace()函数进行替换。


虽然这个方法有点笨重,但是思路还是很清晰,不过后来我发现如果xml中有多个标签里面包含'<'的时候这个方法就不是很好用了。


于是乎,想到了正则回调的方法:

function search($match){
  $match = str_replace("<", "@", $match[1]); 
 return $match;  
}

//$xml = simplexml_load_file("tes.xml");        //读取xml文件
$xml_str=file_get_contents("test.xml");
$xml_result=preg_replace_callback("/<a>(.*?)<\/a>/s", "search", $xml_str);//正则回调
$xml = simplexml_load_string($xml_result); 
print_r($xml);exit;


简单说下思路:

主要就是用正则回调把尖括号< 替换为@ 返回读取时 再把@替换回来为<


这样就简单的解决了!!!

WebServiceXML 结构中,<types> 标签用于定义 WebService 中使用的数据类型,是在 WSDL 中的一个重要分。<types> 标签中定义的数据类型可以在 WebService 的其他部分中使用,例如在 <message>、<portType>、<binding> <service> 中。 如果你想要将 <types> 标签中定义的参数放到 <body> 中,需要按照以下步骤进行操作: 1. 在 WSDL 的 <message> 中定义请求响应消息,包括消息名称参数。例如: ```xml <message name="GetWeatherRequest"> <part name="city" type="xsd:string"/> </message> <message name="GetWeatherResponse"> <part name="weather" type="xsd:string"/> </message> ``` 2. 在 <portType> 中定义 WebService 的接口,包括接口名称、输入输出参数、操作等。例如: ```xml <portType name="WeatherServicePortType"> <operation name="getWeather"> <input message="tns:GetWeatherRequest"/> <output message="tns:GetWeatherResponse"/> </operation> </portType> ``` 3. 在 <binding> 中定义 WebService 的绑定,包括使用的协议数据格式等。例如: ```xml <binding name="WeatherServiceBinding" type="tns:WeatherServicePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="getWeather"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> ``` 4. 在 <service> 中定义 WebService 的服务端点,包括服务名称、服务地址等。例如: ```xml <service name="WeatherService"> <port name="WeatherServicePort" binding="tns:WeatherServiceBinding"> <soap:address location="http://localhost:8080/WeatherService"/> </port> </service> ``` 在上述过程中,<input> <output> 标签中的 <soap:body> 标签的 use 属性设置为 "literal",表示使用文本格式传输数据。这样,在请求中传递的参数就会被包含在 <soap:body> 中,请求的 XML 结构类似于: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://example.com/weather"> <soapenv:Header/> <soapenv:Body> <ser:getWeather> <ser:city>Beijing</ser:city> </ser:getWeather> </soapenv:Body> </soapenv:Envelope> ``` 在响应中,返回的数据也会被包含在 <soap:body> 中,响应的 XML 结构类似于: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://example.com/weather"> <soapenv:Header/> <soapenv:Body> <ser:getWeatherResponse> <ser:weather>Sunny</ser:weather> </ser:getWeatherResponse> </soapenv:Body> </soapenv:Envelope> ``` 注意,在实际使用中,需要根据实际情况设置 <soap:body> 的 use 属性命名空间等参数,以及根据 WSDL 中的参数描述来设置请求响应的 XML 结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值