Ok, so I have figured out how to parse an custom returned XML doc (actually a string that I made into a doc for testing).
At this point I am attempting to integrate my parse routine into my main code and I am having an issue getting it to mesh, and am looking for a little help in how to 1) combine the two and 2) how to catch the incoming xml string.
FYI the string is sent to me as a return message to an xml message that I send first. (does that make sense)
Here is the how I am parsing the xml code…
[PHP]
<?php
function GetXMLTree ($xmldata)
{
// we want to know if an error occurs
ini_set ('track_errors' , '1');
$xmlreaderror = false;
$parser = xml_parser_crea te ('UTF-8');
xml_parser_set_ option ($parser, XML_OPTION_SKIP _WHITE, 1);
xml_parser_set_ option ($parser, XML_OPTION_CASE _FOLDING, 0);
/* if (!xml_parse_int o_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
echo "error";
}
*/ xml_parser_free ($parser);
if (!$xmlreaderror ) {
$result = array ();
$i = 0;
if (isset ($vals [$i]['attributes']))
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
$result [$vals [$i]['tag']] = array_merge ((array)$attrib utes, (array)GetChild ren ($vals, $i, 'open'));
}
ini_set ('track_errors' , '0');
return $result;
}
function GetChildren ($vals, &$i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}
$children = array (); // Contains node data
/* Loop through children */
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
// first check if we already have one and need to create an array
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
// there is one of these things already and it is itself an array
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
$children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
// I don't think I need attributes but this is how I would do them:
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
// now check: do we already have an array or a value?
if (isset ($children [$vals [$i]['tag']])) {
// case where there is an attribute but no value, a complete with an attribute in other words
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
}
// case where there is an array of identical items with attributes
elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else
$children [$vals [$i]['tag']] = $attributes;
}
}
return $children;
}
?>
[/PHP]
This is where I am reading the current Doc...kind of...
[PHP]
include_once(". ./class.xmltoarra y1.php");
$xmlResponse= "
//.....I have the xml data in here....
";
$contents = ($xmlResponse);
$data = GetXMLTree ($contents);
//and I can print using...
echo "<pre>";
print_r ($data);
echo "</pre>";
[/PHP]
Now, if I do something like this
The response is printed directly to the screen....But there is some serious formating that has to take place so this is not a good option. Also, before asking I do not have a db that I can shove it off into...it is all going into named sessions at this point...see below...
So what I am attempting to do is on the page where I generate the XML that I send I do the following...
[PHP]
//Send XML
$xmlResponse = sendXMLString($ xmlString);
//Check for curl error
if ( $xmlResponse == "" )
{
header("Locatio n: " . $GatewaySetting s['ErrorPage'] . "?gateway_error =" . rawurlencode("P rocessing error. Please try again."));
}
else
{ $xmlResponse != "";
$contents = ($xmlResponse);
$data = GetXMLTree ($contents);
// save the generated array to the $_SESSION variable
$_SESSION['xmlResponse'] = $data['NewAdd']['All']['ResponseText'];
//Lots of other sessions being built but will omit for space and read time....
//Redirect to Compete or Faild Page...
if ($_SESSION['xmlResponse'] == "Succeed") {
header('Locatio n:https://192.168.101.169/web_p4/fragments/thankyou.php');
die();
}
else
{
header('Locatio n:http://192.168.101.169/webp4/login.php');
die();
}
}
[/PHP]
Every thing I have tried I am getting no where with...and this last round I get it printed to the screen, comment that out then I get a no database selected error....?!?!?
(yes there is a db that holds the values for the string that I generate and send but not one for me to store the incoming sting in...db guy will not set one up)
I have hit a hard dead line to get this up and going (Nov 14) So any suguestions, assistance etc would be outstanding....
I know that this is a long post but I thank you for getting to the end here...Thanks
At this point I am attempting to integrate my parse routine into my main code and I am having an issue getting it to mesh, and am looking for a little help in how to 1) combine the two and 2) how to catch the incoming xml string.
FYI the string is sent to me as a return message to an xml message that I send first. (does that make sense)
Here is the how I am parsing the xml code…
[PHP]
<?php
function GetXMLTree ($xmldata)
{
// we want to know if an error occurs
ini_set ('track_errors' , '1');
$xmlreaderror = false;
$parser = xml_parser_crea te ('UTF-8');
xml_parser_set_ option ($parser, XML_OPTION_SKIP _WHITE, 1);
xml_parser_set_ option ($parser, XML_OPTION_CASE _FOLDING, 0);
/* if (!xml_parse_int o_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
echo "error";
}
*/ xml_parser_free ($parser);
if (!$xmlreaderror ) {
$result = array ();
$i = 0;
if (isset ($vals [$i]['attributes']))
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
$result [$vals [$i]['tag']] = array_merge ((array)$attrib utes, (array)GetChild ren ($vals, $i, 'open'));
}
ini_set ('track_errors' , '0');
return $result;
}
function GetChildren ($vals, &$i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}
$children = array (); // Contains node data
/* Loop through children */
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
// first check if we already have one and need to create an array
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
// there is one of these things already and it is itself an array
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
$children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
// I don't think I need attributes but this is how I would do them:
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
// now check: do we already have an array or a value?
if (isset ($children [$vals [$i]['tag']])) {
// case where there is an attribute but no value, a complete with an attribute in other words
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
}
// case where there is an array of identical items with attributes
elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else
$children [$vals [$i]['tag']] = $attributes;
}
}
return $children;
}
?>
[/PHP]
This is where I am reading the current Doc...kind of...
[PHP]
include_once(". ./class.xmltoarra y1.php");
$xmlResponse= "
//.....I have the xml data in here....
";
$contents = ($xmlResponse);
$data = GetXMLTree ($contents);
//and I can print using...
echo "<pre>";
print_r ($data);
echo "</pre>";
[/PHP]
Now, if I do something like this
Code:
echo "<pre>\n"; print_r ($xmlResponse); echo "</pre>\n"; exit;
So what I am attempting to do is on the page where I generate the XML that I send I do the following...
[PHP]
//Send XML
$xmlResponse = sendXMLString($ xmlString);
//Check for curl error
if ( $xmlResponse == "" )
{
header("Locatio n: " . $GatewaySetting s['ErrorPage'] . "?gateway_error =" . rawurlencode("P rocessing error. Please try again."));
}
else
{ $xmlResponse != "";
$contents = ($xmlResponse);
$data = GetXMLTree ($contents);
// save the generated array to the $_SESSION variable
$_SESSION['xmlResponse'] = $data['NewAdd']['All']['ResponseText'];
//Lots of other sessions being built but will omit for space and read time....
//Redirect to Compete or Faild Page...
if ($_SESSION['xmlResponse'] == "Succeed") {
header('Locatio n:https://192.168.101.169/web_p4/fragments/thankyou.php');
die();
}
else
{
header('Locatio n:http://192.168.101.169/webp4/login.php');
die();
}
}
[/PHP]
Every thing I have tried I am getting no where with...and this last round I get it printed to the screen, comment that out then I get a no database selected error....?!?!?
(yes there is a db that holds the values for the string that I generate and send but not one for me to store the incoming sting in...db guy will not set one up)
I have hit a hard dead line to get this up and going (Nov 14) So any suguestions, assistance etc would be outstanding....
I know that this is a long post but I thank you for getting to the end here...Thanks
Comment