我很难删除XML开头和结尾标签之间以及标签之间的空格。从XML代码中可以看到,
我尝试了对preg_replace(),trim()的几种修改,但似乎无法使其正常工作。我了解通常,应该拒绝无效的XML而不是尝试对其进行修复,但是我必须对此进行修复。
XML
< priip>
< data>
< product>
< priipCloudProductTemplate>otc</priipCloudProductTemplate>
< priipCloudProductType>fxSwap</priipCloudProductType>
< productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> < /product>
< 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> .......
我希望输出没有多余的空格
输出XML
<priip>
<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier></product>
<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> ......
答案 0 :(得分:1)
也许像这样...
$xml = '< priip>
< data>
< product>
< priipCloudProductTemplate>otc</priipCloudProductTemplate>
< priipCloudProductType>fxSwap</priipCloudProductType>
< productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> < /product>
< 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>';
echo preg_replace('/(<)\s(\/?(?:[A-Z][A-Z\d]*)\b[^>]*>)/i', '$1$2', $xml);
/*
* Will produce...
<priip>
<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> </product>
<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>
*/
即使标签包含< tag arg="val">
之类的属性,标签也可以工作,而其他解决方案不会,不幸的是,由于我的声誉低下,我无法评论他的回答...
答案 1 :(得分:1)
我认为它可以帮助您。
<?php
$xml = <<<XML
< priip>
< data>
< product>
< priipCloudProductTemplate>otc</priipCloudProductTemplate>
< priipCloudProductType>fxSwap</priipCloudProductType>
< productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> < /product>
< 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> .......
XML;
$x = preg_replace('/(<)(\s+)\/?(\w+\>)/m', "$1$3", $xml);
echo $x;
?>
返回XML
<priip>
<data>
<product>
<priipCloudProductTemplate>otc</priipCloudProductTemplate>
<priipCloudProductType>fxSwap</priipCloudProductType>
<productIdentifier>RBI_fxSwap_EURUSD_long_1Y2D_EUR</productIdentifier> <product>
<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> .......
答案 2 :(得分:0)
您可以使用带有数组映射的简单php private val fSingletonClass
来执行操作,以删除带有str_replace()
的{{1}}和带有>space
的{{1}}
>