如何从xml对象中提取值?

时间:2016-05-26 16:24:20

标签: php xml printing display serializer

我正在尝试从这个XML($ xml)打印一个值,我尝试了每一种方式来做但没有。

我正在使用ASMX Web服务,因此我必须解析对XML的对象响应,并且我使用XMLSerializer来执行此操作。

我只是想知道从整个XML打印单个值我做错了什么。

这是我的代码:

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);

希望你能帮帮我,谢谢。

修改

最后我想出了最后一步。这是最终的代码:

<?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;
?>

感谢@Rasclatt的大力帮助。

1 个答案:

答案 0 :(得分:1)

大概$xml的结果是字符串形式(虽然,它可能来自__toString()方法而不是真正的字符串,所以如果不将字符串输出到输出,此方法可能无法工作首先缓冲(也就是说,我确定,不是该类的目的)但是你可以在那时使用simplexml_load_string()

print_r(simplexml_load_string($xml));

只是图片的一部分:

$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));

给你:

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
)

我确信XML_Serializer类有一个xml->array和/或xml->object,所以您希望在该类文件中找到该方法。

修改 我注意到您正在向页面回显浏览器安全标记,您需要使用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;