简体   繁体   中英

How to extract a value from an xml object?

I am trying to print a value from this XML ($xml), I tried every single way to do it but nothing.

I am using ASMX Web services so i had to parse the object response to XML and i used XMLSerializer to do it.

I just want to know what i am doing wrong to print a single value from entire XML.

Here is my code:

require_once('nusoap/lib/nusoap.php');
require_once 'xmlserializer/XML/Serializer.php';
$client = new SoapClient('https://omegasandbox.megasys.net:444/Client/WebServices/CustomerPortal.asmx?WSDL');
$token = $client->Login(array('clientNumber' => 'xxx', 'userName' => 'xxxxxx', 'password' => 'xxxxxxx'));

echo '<br><br>';

$realtoken = $token->LoginResult;
$summary = $client->GetAccountSummary(array('token'=>$realtoken, 'accountNumber' => 1111));
$summary = json_encode($summary);
$data = json_decode($summary, true);

// An array of serializer options.
$serializer_options = array (
  'addDecl' => TRUE,
  'encoding' => 'ISO-8859-1',
  'indent' => '  ',
  'rootName' => 'json',
  'mode' => 'simplexml'
); 

$Serializer = &new XML_Serializer($serializer_options);
$status = $Serializer->serialize($data);

if (PEAR::isError($status)) {
  die($status->getMessage());
}

$xml = $Serializer->getSerializedData();

echo "echo xml <br><br>";
echo $xml; 
echo "<br><br>";
echo "echo xml2<br><br>";
echo $xml2 =  htmlspecialchars_decode($xml);

Hope you can help me, thank you.


EDIT:

Finally I figure it out the final step. Here is the final code:

<?php 
require_once('nusoap/lib/nusoap.php');
require_once 'xmlserializer/XML/Serializer.php';
$client = new SoapClient('https://omegasandbox.megasys.net:444/Client/WebServices/CustomerPortal.asmx?WSDL');
$token = $client->Login(array('clientNumber' => 'xxx', 'userName' => 'xxxxx', 'password' => 'xxxxxx'));

echo '<br><br>';

$realtoken = $token->LoginResult;
$summary = $client->GetAccountSummary(array('token'=>$realtoken, 'accountNumber' => 11111));
$summary = json_encode($summary);
$data = json_decode($summary, true);

// An array of serializer options.
$serializer_options = array (
  'addDecl' => TRUE,
  'encoding' => 'ISO-8859-1',
  'indent' => '  ',
  'rootName' => 'json',
  'mode' => 'simplexml'
); 

$Serializer = &new XML_Serializer($serializer_options);
$status = $Serializer->serialize($data);

if (PEAR::isError($status)) {
  die($status->getMessage());
}

$xml = $Serializer->getSerializedData();

ob_start();
echo $xml;
$data = ob_get_contents();
ob_end_clean();
$order = simplexml_load_string(htmlspecialchars_decode($data));

//This was the final step.
echo $order->GetAccountSummaryResult[0]->any[0]->GetAccountSummary[0]->LoanAmount;
?>

Thanks to @Rasclatt for the big help.

Presumably the results of $xml is in a string form (although, it's probably coming from a __toString() method and not really a string, so this method may not work without outputting the string to a output buffer first (which is, I'm sure, not what the class is intending) but you could use simplexml_load_string() at that point:

print_r(simplexml_load_string($xml));

Just a portion of your image:

$xml = '<GetAccountSummary xmlns="">
    <OpenDate>02/19/2016</OpenDate>
    <LoanAmount>$15,000.00</LoanAmount>
    <Tenn>48</Tenn>
    <Frequency>Monthly</Frequency>
    <Rate>23. 9900 %</Rate>
    <PaymentAmount>$488.94</PaymentAmount>
    <NextDueDate>03/19/2016</NextDueDate>
    <NextAmountDue>$488.94</NextAmountDue>
    <CurrentBalance>$15,000.00</CurrentBalance>
    <UnpaidLateFees>$0.00</UnpaidLateFees>
    <UnpaidOtherFees>$0.00</UnpaidOtherFees>
</GetAccountSummary>';

print_r(simplexml_load_string($xml));

Gives you:

SimpleXMLElement Object
(
    [OpenDate] => 02/19/2016
    [LoanAmount] => $15,000.00
    [Tenn] => 48
    [Frequency] => Monthly
    [Rate] => 23. 9900 %
    [PaymentAmount] => $488.94
    [NextDueDate] => 03/19/2016
    [NextAmountDue] => $488.94
    [CurrentBalance] => $15,000.00
    [UnpaidLateFees] => $0.00
    [UnpaidOtherFees] => $0.00
)

I am sure the XML_Serializer class has an xml->array and/or xml->object so you will want that to locate that method in that class file(s).


EDIT I noticed you are echoing browser-safe markup to your page, you need to decode it back with htmlspecialchars_decode() :

$xml = $Serializer->getSerializedData();
ob_start();
echo $xml;
$data = ob_get_contents();
ob_end_clean();

$order = simplexml_load_string(htmlspecialchars_decode($data));
echo $order->OpenDate;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM