...

View Full Version : How to parse xml response not sure if right forum...



nine72
09-29-2008, 11:00 PM
Hi, I have this massive (4000 plus lines of code) generating a huge XML string (500 to 1000 tags). My back end system reply’s to me via an xml response. The response can contain ALL of my xml tags if there is an error with the input.

If everything is good then I get a very short xml response…

I need to take and parse the return string and print the results out to ether a complete or failed page that is in php.

Here is a sample of the error response…

<Response>
<Transaction>NewAdd</Transaction>
<SessionId>46A644041DD3EDC696B68CD503AABCAB</SessionId>
<ResponseText>Failed</ResponseText>
<ErrCode>ERROR_BL_00023</ErrCode>
<ErrMsg>
Location:
<device_category>HANDHELD</device_category>
<device_name>BLAKBERRY</device_name>
<device_model>82XX</device_model>
<device_application>FINANCEING</device_application>
<device_gateway>NODE42</device_gateway>
<device_comm_format>LANDLINE</device_comm_format>
<device_mode>W</device_mode>
<device_dial_up>N</device_dial_up>
<device_flag>Y</device_flag>
Description: can NOT find any udid from our database system.
Solution: please check information listed above.
</ErrMsg>
</Response>


This is the routine that I am working with to handle the response….


//Send XML
$xmlResponse = sendXMLString($xmlString);

//Check for curl error
if ( $xmlResponse == "" )
{
header("Location: " . $GatewaySettings['CommitFailed'] . "?gateway_error=") . rawurlencode("Error Loading KIOSK. Please try again.");
}
else
{
// Parse XML response
$xml_parser = xml_parser_create();
// set to not change to uppercase
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
// ignore white space
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
// puts values in an array of xml tags (vals)
xml_parse_into_struct($xml_parser, $xmlResponse, $vals, $index);
xml_parser_free($xml_parser);

$retTransaction = "";
$retSessionId = "";
$retErrCode = "";
$retErrMsg = "";
$retResponce = "";

$numTags = $index[Response][1]; // number of tags including Response
$numTags = 6;

for ($x=0; $x<$numTags; $x++)
{
$key = $vals[$x][tag];

switch ($key)
{
case "Transaction":
$retTransaction = $vals[$x][value];
break;
case "Transaction ID":
$retSessionId = $vals[$x][value];
break;
case "Responce":
$retResponce = $vals[$x][value];
break;
case "Error Code":
$retErrCode = $vals[$x][value];
break;
case "Error Message":
$retErrMsg = $vals[$x][value];
break; //there can be as manay as 1000 "tags" in the Error Message.
default:
// if other message could be error
$retErrMsg = $vals[$x][value];
break;
} // end switch
} // end for loop

if ( $retResponce == "SUCCEED")
{
header("Location: " . $GatewaySettings[‘LoadedPage'] . "?Responce=" . rawurlencode($retResponce) . "&ApprovalCode=" . rawurlencode($retResponce));
}
else
{
//Function that takes retActionCode -> String based on code
header("Location: " . $GatewaySettings['FailedPage'] . "?gateway_error=" . rawurlencode($retResponce) . "&ErrorMsg=" . rawurlencode($retErrMsg));
}
}
}
else
{
header("Location: " . $GatewaySettings['FailedPage'] . "?gateway_error=" . rawurlencode($retErrMsg));
}


Any ideas, suggestions, samples, or edits are welcome…

Thanks!

oesxyl
09-30-2008, 04:06 AM
this is not valid xml:

<Response>
<Transaction>NewAdd</Transaction>
<SessionId>46A644041DD3EDC696B68CD503AABCAB</SessionId>
<ResponseText>Failed</ResponseText>
<ErrCode>ERROR_BL_00023</ErrCode>
<ErrMsg>
Location:
<device_category>HANDHELD</device_category>
<device_name>BLAKBERRY</device_name>
<device_model>82XX</device_model>
<device_application>FINANCEING</device_application>
<device_gateway>NODE42</device_gateway>
<device_comm_format>LANDLINE</device_comm_format>
<device_mode>W</device_mode>
<device_dial_up>N</device_dial_up>
<device_flag>Y</device_flag>
Description: can NOT find any udid from our database system.
Solution: please check information listed above.
</ErrMsg>
</Response>


If I understand the problem you could use xslt to process the xml and build the page you want. Look into the php manual of DOM extension.

best regards

oesxyl
09-30-2008, 04:15 AM
I just see a typo:


if ( $retResponce == "SUCCEED")
{
header("Location: " . $GatewaySettings[‘LoadedPage'] . "?Responce=" . rawurlencode($retResponce) . "&ApprovalCode=" . rawurlencode($retResponce));


must be:


if ( $retResponce == "SUCCEED")
{
header("Location: " . $GatewaySettings['LoadedPage'] . "?Responce=" . rawurlencode($retResponce) . "&ApprovalCode=" . rawurlencode($retResponce));



best regards

nine72
09-30-2008, 04:21 PM
Thanks for the info,
I have given the not valid part to the person handling the response part.
Fixed the typo this morning but it is not working as needed.

For reasons that have not been explained to me...and they never are explained to me….
I can not use DOM or SAX to parse it out….

I have a set of pages where this is working, it is an old form for credit card transactions.
I uses the same methods to pass and receive, that parses the response just fine and sends to the correct page. I was attempting to build off that process. I guess I will have to dig a bit deeper…

oesxyl
09-30-2008, 05:11 PM
Thanks for the info,
I have given the not valid part to the person handling the response part.
Fixed the typo this morning but it is not working as needed.

For reasons that have not been explained to me...and they never are explained to me….
I can not use DOM or SAX to parse it out….

I have a set of pages where this is working, it is an old form for credit card transactions.
I uses the same methods to pass and receive, that parses the response just fine and sends to the correct page. I was attempting to build off that process. I guess I will have to dig a bit deeper…
try to check if xml response is not truncated, this could be an effect of using GET method instead of POST.

best regards

nine72
09-30-2008, 07:18 PM
badabing-o
that was it, I had them make a small change and it is all working now..

Thanks Oesxyl!:thumbsup:

now how to close this topic....

oesxyl
09-30-2008, 09:42 PM
badabing-o
that was it, I had them make a small change and it is all working now..

Thanks Oesxyl!:thumbsup:

now how to close this topic....
I'm glad that is solved, :)
To close the thread, you can edit it and add a prefix "resolved", :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum