1
如何解析XML到JSON數組?如何將XML解析爲JSON數組?
<?php
use SoapClient;
$params = '<soapenv:Envelope xmlns:soapenv=\"" xmlns:qde=\"http://qde.service.los\" xmlns:xsd=\"http://to.service.los/xsd\">\n <soapenv:Header/>\n <soapenv:Body>\n <qde:invoke>\n <!--Optional:-->\n <qde:losRequest>\n <!--Optional:-->\n <xsd:data><![CDATA[<ApplicationIngestionRequest>\n <BranchCode></BranchCode>\n <SourceCode>BANEAP</SourceCode>\n <SECode>MP</SECode>\n <PromoCode>SCEA</PromoCode>\n <PromoGroup>EA</PromoGroup>\n <ChannelCode>EAPL</ChannelCode>\n <Salutation>1</Salutation>\n <FirstName>muthu</FirstName>\n <MiddleName>krishnan</MiddleName>\n <LeadRefNo></LeadRefNo>\n <LastName>nataraj</LastName>\n <DOB>18/11/1992</DOB>\n <Mobile>9698741270</Mobile>\n <Gender>M</Gender>\n <Qualification>Btech</Qualification>\n <PAN>CLDPM3183L</PAN>\n <EmailAddress>[email protected]</EmailAddress>\n <ResiAddress1>FLAT NO -7 G N VENAKTESH BLDG, KENCHARED</ResiAddress1>\n <ResiAddress2>CHANNASANDRA</ResiAddress2>\n <ResiAddress3></ResiAddress3>\n <ResiCity>BANGALORE</ResiCity>\n <ResiState>KTK</ResiState>\n <ResiPin>560067</ResiPin>\n <ResiPhone>22651687</ResiPhone>\n <ResiStdCode>080</ResiStdCode>\n <OccupationType>S</OccupationType>\n <AlternateCardNo></AlternateCardNo>\n <Designation>ASSOCIATE SYSTEM ENGINEER</Designation>\n <CompanyName>GE CAPITAL</CompanyName>\n <NatureOfBusiness></NatureOfBusiness>\n <OfficeAddress1>PRESTIGE TECH PARK</OfficeAddress1>\n <OfficeAddress2>KADDUBISNAHALLI</OfficeAddress2>\n <OfficeAddress3></OfficeAddress3>\n <OfficePhone>42240800</OfficePhone>\n <OfficeStdCode>080</OfficeStdCode>\n <OfficeState>KTK</OfficeState>\n <OfficeCity>BANGALORE</OfficeCity>\n <YearsOfCurrentEmployment></YearsOfCurrentEmployment>\n <CardType>SSU2</CardType>\n <OfficePin>560037</OfficePin>\n <TextSpareField1>MP123</TextSpareField1>\n <TextSpareField2></TextSpareField2>\n <TextSpareField3></TextSpareField3>\n <TextSpareField4></TextSpareField4>\n <TextSpareField5></TextSpareField5>\n <FathersName></FathersName>\n <NumericSpareField1>1099796622</NumericSpareField1>\n <NumericSpareField2>123</NumericSpareField2>\n <NumericSpareField3>123</NumericSpareField3>\n <NumericSpareField4>123</NumericSpareField4>\n <NumericSpareField5>123</NumericSpareField5>\n <DateSpareField1>12/12/2012</DateSpareField1>\n <DateSpareField2>12/12/2012</DateSpareField2>\n <DateSpareField3>12/12/2012</DateSpareField3>\n <DateSpareField4>12/12/2012</DateSpareField4>\n <DateSpareField5>12/12/2012</DateSpareField5>\n <EmployeePFNumber>234234</EmployeePFNumber>\n <BranchManagerPFNumber></BranchManagerPFNumber>\n <PriorityFlag></PriorityFlag>\n <DateOfPickup></DateOfPickup>\n <TimeOfPickup></TimeOfPickup>\n <StatementPreference>E</StatementPreference>\n <LoyaltyChannel></LoyaltyChannel>\n <LoyaltyNumber></LoyaltyNumber>\n <ActualFulfillmentDeviation>N</ActualFulfillmentDeviation>\n <PhysicalAlreadyReceived>N</PhysicalAlreadyReceived> \n <MothersMaidenName></MothersMaidenName>\n <ResiLandmark></ResiLandmark>\n <OfficeLandmark></OfficeLandmark>\n </ApplicationIngestionRequest>]]></xsd:data>\n <!--Optional:-->\n <xsd:requestid>?</xsd:requestid>\n <!--Optional:-->\n <xsd:userCtx>\n <!--Optional:-->\n <xsd:password>1234</xsd:password>\n <!--Optional:-->\n <xsd:userId>00000000</xsd:userId>\n </xsd:userCtx>\n </qde:losRequest>\n </qde:invoke>\n </soapenv:Body>\n</soapenv:Envelope>';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "http://servicetest.gecapital1.glb.gemoney.in/DevWebService/LOSWebApp/services/ApplicationIngestionService?wsdl=",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $params,
CURLOPT_HTTPHEADER => [
"Content-Type: text/xml",
"SOAPAction: {$SOAPAction}"
],
]);
$response123 = curl_exec($ch);
if (!function_exists('xmlToArray')) {
function xmlToArray($xmlInput, $bodyXpath) {
$response = html_entity_decode(preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlInput));
$test = simplexml_load_string($response, null, LIBXML_NOCDATA);
$xmlTest = new SimpleXMLElement($test->soapenvBody->qdeinvoke->qdelosRequest->xsddata);
return json_decode(json_encode((array)$test), true);
}
}
print_r(xmlToArray($response123, '//soapenvBody'));
什麼問題? – AbraCadaver
你不能只是將你的XML轉換爲PHP中的數組,然後'json_encode()'它? – Nicolas
我已經嘗試過很多可能的PHP內置函數,沒有任何幫助需要。然後我跳上了上面的路。在行中拋出錯誤** $ xmlTest = new SimpleXMLElement($ test-> soapenvBody-> qdeinvoke-> qdelosRequest-> xsddata); ** –