[英]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.我试图从这个 XML ($xml) 打印一个值,我尝试了每一种方法来做到这一点,但什么也没做。
I am using ASMX Web services so i had to parse the object response to XML and i used XMLSerializer to do it.我正在使用 ASMX Web 服务,所以我必须解析对 XML 的对象响应,我使用 XMLSerializer 来完成它。
I just want to know what i am doing wrong to print a single value from entire XML.我只想知道从整个 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.感谢@Rasclatt 的大力帮助。
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:大概
$xml
的结果是字符串形式(尽管它可能来自__toString()
方法而不是真正的字符串,因此如果不先将字符串输出到输出缓冲区,此方法可能无法工作(即,我'我可以肯定,这不是类的意图),但此时您可以使用simplexml_load_string()
:
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).我确信
XML_Serializer
类有一个xml->array
和/或xml->object
所以你会希望在该类文件中找到该方法。
EDIT I noticed you are echoing browser-safe markup to your page, you need to decode it back with htmlspecialchars_decode()
:编辑我注意到您正在向您的页面回显浏览器安全标记,您需要使用
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.