通过 SOAP 响应解析
Parse through SOAP response
我是 SOAP 的初学者,一直在尝试弄清楚如何在调用 SOAP 时解析 XML 响应 API.I 已经在 Whosebug 上尝试过类似的问题,但不知何故不适合我。我想循环访问 Village 和 VillageName 标签。
<dsVillages>
<xs:schema id="dsVillages" targetNamespace="http://app.in/wsror/dsVillages.xsd" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="dsVillages" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="VILLAGES">
<xs:complexType>
<xs:sequence>
<xs:element name="taluka" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Village" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="VillageName" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram>
<dsVillages>
<VILLAGES diffgr:id="VILLAGES1" msdata:rowOrder="0">
<taluka>0100</taluka>
<Village>0192</Village>
<VillageName>Adcol </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES2" msdata:rowOrder="1">
<taluka>0100</taluka>
<Village>1240</Village>
<VillageName>Bando </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES3" msdata:rowOrder="2">
<taluka>0100</taluka>
<Village>0207</Village>
<VillageName>Beto </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES4" msdata:rowOrder="3">
<taluka>01006</taluka>
<Village>0189</Village>
<VillageName>Betqi </VillageName>
</VILLAGES>
</dsVillages>
</diffgr:diffgram>
</dsVillages>
我正在使用以下代码从 API'
获取响应
$url = "http://test.asmx?wsdl";
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetAllVill xmlns="http://apps.in/wsror">
<strTaluka>code</strTaluka>
<Username>username</Username>
<Password>password</Password>
</GetAllVill>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
如果我使用以下方法,我可以一次访问 1 个值
$doc = new DOMDocument();
$doc->loadXML($data);
echo $doc->getElementsByTagName('VillageName')->item(1)->nodeValue;
上述方法的输出是:
Bando
我需要循环访问 Village 和 VillageName 标签。
我已经使用以下方法访问了所需的标签。请注意我正在使用选项标签,因为这些将用作下拉选项
$VILLAGES = $doc->getElementsByTagName('VILLAGES');
foreach ($VILLAGES as $VILLAGE) {
$VillageName = $VILLAGE->getElementsByTagName('VillageName')->item(0)->nodeValue;
$Village = $VILLAGE->getElementsByTagName('Village')->item(0)->nodeValue;
// $id = $VILLAGE->getElementsByTagName('taluka')->item(0)->nodeValue;
echo '<option value="'.htmlentities($Village, ENT_QUOTES, "UTF-8").'" >'.htmlentities($VillageName, ENT_QUOTES, "UTF-8").'</option> ';
}
我是 SOAP 的初学者,一直在尝试弄清楚如何在调用 SOAP 时解析 XML 响应 API.I 已经在 Whosebug 上尝试过类似的问题,但不知何故不适合我。我想循环访问 Village 和 VillageName 标签。
<dsVillages>
<xs:schema id="dsVillages" targetNamespace="http://app.in/wsror/dsVillages.xsd" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="dsVillages" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="VILLAGES">
<xs:complexType>
<xs:sequence>
<xs:element name="taluka" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Village" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="VillageName" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="30"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram>
<dsVillages>
<VILLAGES diffgr:id="VILLAGES1" msdata:rowOrder="0">
<taluka>0100</taluka>
<Village>0192</Village>
<VillageName>Adcol </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES2" msdata:rowOrder="1">
<taluka>0100</taluka>
<Village>1240</Village>
<VillageName>Bando </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES3" msdata:rowOrder="2">
<taluka>0100</taluka>
<Village>0207</Village>
<VillageName>Beto </VillageName>
</VILLAGES>
<VILLAGES diffgr:id="VILLAGES4" msdata:rowOrder="3">
<taluka>01006</taluka>
<Village>0189</Village>
<VillageName>Betqi </VillageName>
</VILLAGES>
</dsVillages>
</diffgr:diffgram>
</dsVillages>
我正在使用以下代码从 API'
获取响应$url = "http://test.asmx?wsdl";
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetAllVill xmlns="http://apps.in/wsror">
<strTaluka>code</strTaluka>
<Username>username</Username>
<Password>password</Password>
</GetAllVill>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($xml),
"Connection: close",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
如果我使用以下方法,我可以一次访问 1 个值
$doc = new DOMDocument();
$doc->loadXML($data);
echo $doc->getElementsByTagName('VillageName')->item(1)->nodeValue;
上述方法的输出是:
Bando
我需要循环访问 Village 和 VillageName 标签。
我已经使用以下方法访问了所需的标签。请注意我正在使用选项标签,因为这些将用作下拉选项
$VILLAGES = $doc->getElementsByTagName('VILLAGES');
foreach ($VILLAGES as $VILLAGE) {
$VillageName = $VILLAGE->getElementsByTagName('VillageName')->item(0)->nodeValue;
$Village = $VILLAGE->getElementsByTagName('Village')->item(0)->nodeValue;
// $id = $VILLAGE->getElementsByTagName('taluka')->item(0)->nodeValue;
echo '<option value="'.htmlentities($Village, ENT_QUOTES, "UTF-8").'" >'.htmlentities($VillageName, ENT_QUOTES, "UTF-8").'</option> ';
}