I am new to xml. I have string response from api as :
<PPResponse Result="000" Key="110308f9-6b67-422b-9dee-9c9da77d8197">
<ResultMessage>Operation is succesfully completed</ResultMessage>
<UtilityInfo>
<UtilityCode>123</UtilityCode>
</UtilityInfo>
<BillInfo>
<Bill>
<BillNumber>110308f9-6b67-422b-9d13-1c9da77d8197</BillNumber>
<DueDate>2015-12-10T07:31:44</DueDate>
<Amount>100</Amount>
<ReserveInfo>test</ReserveInfo>
<BillParam>
<mask>4</mask>
<commission type="0" val="0.00" op="-" paysource="1" />
</BillParam>
<RefStan>123123123123</RefStan>
</Bill>
</BillInfo>
</PPResponse>
I try to convert it to xml object like this:
`
libxml_use_internal_errors(true);
$simple_xml = simplexml_load_string($response);
if ($simple_xml === false) {
echo "Failed loading XML
";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
echo 'hi'; exit();
}
echo 'pass'; exit();`
It displays pass. Now, the error is that, when I dd($simple_xml), I get nothing as:
libxml_use_internal_errors(true); $simple_xml = simplexml_load_string($response); if ($simple_xml === false) { echo "Failed loading XML "; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } echo 'hi'; exit(); } dd($simple_xml)
I need to access the keys and values from the string after converting it to xml object. I think, regex is also solution, but I need to do through xml object.
Any help would be highly appreciated. Its a sort of emergency.
This works fine for me:
libxml_use_internal_errors(true);
$xml = simplexml_load_string($response);
// var_dump($xml); this will output the xml object just fine
if ($xml !== false) {
echo "Successfuly loaded the XML" . PHP_EOL;
print "Message: " . $xml->ResultMessage . PHP_EOL;
print "Utility code: " . $xml->UtilityInfo->UtilityCode . PHP_EOL;
print "Bill Amount: " . $xml->BillInfo->Bill->Amount . PHP_EOL;
}
else{
echo "Failed loading the XML" . PHP_EOL;
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
Will display
Successfuly loaded the XML
Message: Operation is succesfully completed
Utility code: 78
Bill Amount: 0
use the below code to test for BOM sequence and remove it if it exists
$bom = pack("CCC", 0xef, 0xbb, 0xbf);
if (0 === strncmp($response, $bom, 3)) {
echo "BOM detected - file is UTF-8" . PHP_EOL;
$response = substr($response, 3);
}
// or
$response = str_replace("\xEF\xBB\xBF",'',$reponse);