将XML涵盖到Json PHP [英] covering XML to Json PHP
本文介绍了将XML涵盖到Json PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试不使用json_encode将XML转换为JSON.我已经写了一些PHP代码来转换它,但是它不能正常工作.显示的是数据标题,但未显示数据本身.有点难以解释,但请自己运行,您将明白我的意思.
I am trying to convert XML to JSON without using json_encode. I have written some PHP code bellow to convert it but it is not working properly. The title for data is showing but not the data itself. Abit hard to explain but please run it yourself and you will see what I mean.
这是一项潜在工作的任务,他们说不要使用json_encode.这是面试过程的一部分.下面是我的代码:
This is a task for a potential job and they have said not to use json_encode. It is part of the interview process. Below is my code:
$data = '<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier>
</product>
<manufacturer>
<id>rbi</id>
<nameLong>Raiffeisen Bank International AG</nameLong>
<nameShort>RBI</nameShort>
<address>Am Stadtpark 9, 1030 Wien, Austria</address>
<telephoneNumber>+43 1 71707 0</telephoneNumber>
<website>http://www.rbinternational.com</website>
<email>complaints@rbinternational.com</email>
</manufacturer>
<document>
<type>final</type>
</document>
<properties>
<includeEarlyRedemptionInExtraordinaryEventsAlert>true</includeEarlyRedemptionInExtraordinaryEventsAlert>
</properties>
<tradeDate>2018-01-18</tradeDate>
<effectiveDate>2018-01-20</effectiveDate>
<fxSwap>
<holder>client</holder>
<currencyPair>EURUSD</currencyPair>
<notionalAmount>1000000</notionalAmount>
<notionalAmountCurrency>EUR</notionalAmountCurrency>
<terminationDate>2019-01-20</terminationDate>
<forwardRate>
<value>1.25620</value>
</forwardRate>
<spotRate>
<value>1.2207</value>
</spotRate>
</fxSwap>
<costs>
<entryCosts>0.0025</entryCosts>
</costs>
<riskMeasures version="v1.0">
<sriRelatedValues>
<valueAtRisk>0</valueAtRisk>
<valueAtRiskEquivalentVolatility>0</valueAtRiskEquivalentVolatility>
<mrm>7</mrm>
<crm>2</crm>
<sri>7</sri>
</sriRelatedValues>
<performanceScenariosRelatedValues>
<positiveScenarioPayoutRHP>11139.633068665</positiveScenarioPayoutRHP>
<positiveScenarioActualReturnRHP>0.1139633069</positiveScenarioActualReturnRHP>
<positiveScenarioAverageReturnPerYearRHP>0.114276</positiveScenarioAverageReturnPerYearRHP>
<positiveScenarioPayoutIHP1>null</positiveScenarioPayoutIHP1>
<positiveScenarioActualReturnIHP1>null</positiveScenarioActualReturnIHP1>
<positiveScenarioAverageReturnPerYearIHP1>null</positiveScenarioAverageReturnPerYearIHP1>
<positiveScenarioPayoutIHP2>null</positiveScenarioPayoutIHP2>
<positiveScenarioActualReturnIHP2>null</positiveScenarioActualReturnIHP2>
<positiveScenarioAverageReturnPerYearIHP2>null</positiveScenarioAverageReturnPerYearIHP2>
<moderateScenarioPayoutRHP>9984.9790016645</moderateScenarioPayoutRHP>
<moderateScenarioActualReturnRHP>-0.0015020998</moderateScenarioActualReturnRHP>
<moderateScenarioAverageReturnPerYearRHP>-0.00150623</moderateScenarioAverageReturnPerYearRHP>
<moderateScenarioPayoutIHP1>null</moderateScenarioPayoutIHP1>
<moderateScenarioActualReturnIHP1>null</moderateScenarioActualReturnIHP1>
<moderateScenarioAverageReturnPerYearIHP1>null</moderateScenarioAverageReturnPerYearIHP1>
<moderateScenarioPayoutIHP2>null</moderateScenarioPayoutIHP2>
<moderateScenarioActualReturnIHP2>null</moderateScenarioActualReturnIHP2>
<moderateScenarioAverageReturnPerYearIHP2>null</moderateScenarioAverageReturnPerYearIHP2>
<negativeScenarioPayoutRHP>8955.6992819847</negativeScenarioPayoutRHP>
<negativeScenarioActualReturnRHP>-0.1044300718</negativeScenarioActualReturnRHP>
<negativeScenarioAverageReturnPerYearRHP>-0.104717</negativeScenarioAverageReturnPerYearRHP>
<negativeScenarioPayoutIHP1>null</negativeScenarioPayoutIHP1>
<negativeScenarioActualReturnIHP1>null</negativeScenarioActualReturnIHP1>
<negativeScenarioAverageReturnPerYearIHP1>null</negativeScenarioAverageReturnPerYearIHP1>
<negativeScenarioPayoutIHP2>null</negativeScenarioPayoutIHP2>
<negativeScenarioActualReturnIHP2>null</negativeScenarioActualReturnIHP2>
<negativeScenarioAverageReturnPerYearIHP2>null</negativeScenarioAverageReturnPerYearIHP2>
<stressScenarioPayoutRHP>6841.9699464563</stressScenarioPayoutRHP>
<stressScenarioActualReturnRHP>-0.3158030054</stressScenarioActualReturnRHP>
<stressScenarioAverageReturnPerYearRHP>-0.316671</stressScenarioAverageReturnPerYearRHP>
<stressScenarioPayoutIHP1>null</stressScenarioPayoutIHP1>
<stressScenarioActualReturnIHP1>null</stressScenarioActualReturnIHP1>
<stressScenarioAverageReturnPerYearIHP1>null</stressScenarioAverageReturnPerYearIHP1>
<stressScenarioPayoutIHP2>null</stressScenarioPayoutIHP2>
<stressScenarioActualReturnIHP2>null</stressScenarioActualReturnIHP2>
<stressScenarioAverageReturnPerYearIHP2>null</stressScenarioAverageReturnPerYearIHP2>
</performanceScenariosRelatedValues>
</riskMeasures>
<costOutputs>
<costsOverTime>
<totalCostsRHP>
<value>24.4219183409</value>
</totalCostsRHP>
<totalCostsIHP1>
<value>null</value>
</totalCostsIHP1>
<totalCostsIHP2>
<value>null</value>
</totalCostsIHP2>
<reductionInActualYieldRHP>
<value>0.0024421918</value>
</reductionInActualYieldRHP>
<reductionInActualYieldIHP1>
<value>null</value>
</reductionInActualYieldIHP1>
<reductionInActualYieldIHP2>
<value>null</value>
</reductionInActualYieldIHP2>
<reductionInYieldRHP>
<value>0.0024489008</value>
</reductionInYieldRHP>
<reductionInYieldIHP1>
<value>null</value>
</reductionInYieldIHP1>
<reductionInYieldIHP2>
<value>null</value>
</reductionInYieldIHP2>
</costsOverTime>
<compositionOfCosts>
<actualEntryCosts>
<value>0.0024421918</value>
</actualEntryCosts>
<actualOtherRecurringCostsPA>
<value>null</value>
</actualOtherRecurringCostsPA>
<actualExitCosts>
<value>0</value>
</actualExitCosts>
<entryCosts>
<value>0.0024489008</value>
</entryCosts>
<otherRecurringCostsPA>
<value>null</value>
</otherRecurringCostsPA>
<exitCosts>
<value>0</value>
</exitCosts>
</compositionOfCosts>
</costOutputs>
</data>';
function json_encoded($a=false)
{
if (is_null($a)) return 'null';
if ($a === false) return 'false';
if ($a === true) return 'true';
if (is_scalar($a))
{
if (is_float($a))
{
// Always use "." for floats.
return floatval(str_replace(",", ".", strval($a)));
}
if (is_string($a))
{
static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
}
if (is_int($a)) { return '"'. $a . '"'; } return $a;
}
$isList = true;
for ($i = 0, reset($a); $i < count($a); $i++, next($a))
{
if (key($a) !== $i)
{
$isList = false;
break;
}
}
$result = array();
if ($isList)
{
foreach ($a as $v) $result[] = json_encoded($v);
return '[' . join(',', $result) . ']';
}
else
{
foreach ($a as $k => $v){ if(is_numeric($k)){ $result[] = '"'.json_encoded($k).'":'.json_encoded($v); }else{ $result[] = json_encoded($k).':'.json_encoded($v); } };
return '{' . join(',', $result) . '}';
}
}
libxml_use_internal_errors(true);
$data = simplexml_load_string($data) or die ("error found");
//if statement to check if data can be loaded
if($data === false){
echo "Unable to load the data";
foreach ($libxml_get_errors() as $error){
echo "<br>", $error->message;
}
}
else{
//below is my code to execute the above function
$json = json_encoded($data);
echo "<pre>";
print_r($json);
echo "</pre>";
}
推荐答案
您可以尝试以下方法:
//xml->array:
$arr=base64_encode(array('note'=>'Tove','from'=>'Jani')); //head........
$json_arr='{"arr":"'.$arr.'"}'; //json
$ARR=json_decode($json_arr,1); //array
$Bascode=base64_decode($ARR['arr']); //base64_decode value
这篇关于将XML涵盖到Json PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文