Bug #70905 [NEW]: SimpleXML parses different w/ newlines and w/o newlines after closing ">"

From: Date: Fri, 13 Nov 2015 01:11:16 +0000
Subject: Bug #70905 [NEW]: SimpleXML parses different w/ newlines and w/o newlines after closing ">"
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197212@lists.php.net to get a copy of this message
From:             be dot creative at webapper dot hu
Operating system: Ubuntu 15.04
PHP version:      5.6.15
Package:          SimpleXML related
Bug Type:         Bug
Bug description:SimpleXML parses different w/ newlines and w/o newlines after closing
">"

Description:
------------
Hi, just ran into this interesting bug, unsure if it effects other PHP
versions. In the test case below you can see that stripped XML parsed
differently than those which are separated by newlines.

Test script:
---------------
<?php
// no indenting and new lines
	$xmlStr = <<<HERE
<?xml version='1.0' encoding='UTF-8'?><ns2RoomRate
RatePlanCode="DEF"
RatePlanID="0:5:0:0"><ns2Rates><ns2Rate UnitMultiplier="1"
/><ns2Rate
UnitMultiplier="1"><ns2TPA_Extensions><ns3AccomSupplement
AmountAfterTax="109.59" AmountBeforeTax="109.59" BestDiscountGrpNo="0"
Compulsory="false" CurrencyCode="USD"
ItemRPH="0:5:0:0S0S"><ns2SupplementDescription Name="*replaced*"
/></ns3AccomSupplement><ns3AccomSupplement AmountAfterTax="0.00"
AmountBeforeTax="0.00" BestDiscountGrpNo="0" Compulsory="true"
CurrencyCode="USD" ItemRPH="0:5:0:0S1S"><ns2SupplementDescription
Name="Child Extra Bed"
/></ns3AccomSupplement></ns2TPA_Extensions></ns2Rate></ns2Rates><ns2RoomRateDescription
Name="Buffet Breakfast" /><ns2Total AmountAfterTax="86.52"
AmountBeforeTax="86.52" CurrencyCode="USD" /></ns2RoomRate>
HERE;
	$xml = simplexml_load_string($xmlStr);
	echo '<pre>'; print_r($xml); echo '</pre>';
	
// with indenting and new lines
	$xmlStr = <<<HERE
<?xml version='1.0' encoding='UTF-8'?>
<ns2RoomRate RatePlanCode="DEF" RatePlanID="0:5:0:0">
	<ns2Rates>
		<ns2Rate UnitMultiplier="1" />
		<ns2Rate UnitMultiplier="1">
			<ns2TPA_Extensions>
				<ns3AccomSupplement AmountAfterTax="109.59" AmountBeforeTax="109.59"
BestDiscountGrpNo="0" Compulsory="false" CurrencyCode="USD"
ItemRPH="0:5:0:0S0S">
					<ns2SupplementDescription Name="*replaced*" />
				</ns3AccomSupplement>
				<ns3AccomSupplement AmountAfterTax="0.00" AmountBeforeTax="0.00"
BestDiscountGrpNo="0" Compulsory="true" CurrencyCode="USD"
ItemRPH="0:5:0:0S1S">
					<ns2SupplementDescription Name="Child Extra Bed" />
				</ns3AccomSupplement>
			</ns2TPA_Extensions>
		</ns2Rate>
	</ns2Rates>
	<ns2RoomRateDescription Name="Buffet Breakfast" />
	<ns2Total AmountAfterTax="86.52" AmountBeforeTax="86.52"
CurrencyCode="USD" />
</ns2RoomRate>
HERE;
	$xml = simplexml_load_string($xmlStr);
	echo '<pre>'; print_r($xml); echo '</pre>';

// no indenting and new lines BUT inserting new lines after!
	$xmlStr = <<<HERE
<?xml version='1.0' encoding='UTF-8'?><ns2RoomRate
RatePlanCode="DEF"
RatePlanID="0:5:0:0"><ns2Rates><ns2Rate UnitMultiplier="1"
/><ns2Rate
UnitMultiplier="1"><ns2TPA_Extensions><ns3AccomSupplement
AmountAfterTax="109.59" AmountBeforeTax="109.59" BestDiscountGrpNo="0"
Compulsory="false" CurrencyCode="USD"
ItemRPH="0:5:0:0S0S"><ns2SupplementDescription Name="*replaced*"
/></ns3AccomSupplement><ns3AccomSupplement AmountAfterTax="0.00"
AmountBeforeTax="0.00" BestDiscountGrpNo="0" Compulsory="true"
CurrencyCode="USD" ItemRPH="0:5:0:0S1S"><ns2SupplementDescription
Name="Child Extra Bed"
/></ns3AccomSupplement></ns2TPA_Extensions></ns2Rate></ns2Rates><ns2RoomRateDescription
Name="Buffet Breakfast" /><ns2Total AmountAfterTax="86.52"
AmountBeforeTax="86.52" CurrencyCode="USD" /></ns2RoomRate>
HERE;
	$xmlStr = str_replace('><', '>'."\n".'<',
$xmlStr); // inserting here
	$xml = simplexml_load_string($xmlStr);
	echo '<pre>'; print_r($xml); echo '</pre>';


Expected result:
----------------
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [RatePlanCode] => DEF
            [RatePlanID] => 0:5:0:0
        )

    [ns2Rates] => SimpleXMLElement Object
        (
            [ns2Rate] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                            [ns2TPA_Extensions] => SimpleXMLElement
Object
                                (
                                    [ns3AccomSupplement] => Array
                                        (
                                            [0] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 109.59
                                                           
[AmountBeforeTax] => 109.59
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> false
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S0S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => *replaced*
                                                                )

                                                        )

                                                )

                                            [1] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 0.00
                                                           
[AmountBeforeTax] => 0.00
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> true
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S1S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => Child Extra Bed
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [ns2RoomRateDescription] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Name] => Buffet Breakfast
                )

        )

    [ns2Total] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [AmountAfterTax] => 86.52
                    [AmountBeforeTax] => 86.52
                    [CurrencyCode] => USD
                )

        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [RatePlanCode] => DEF
            [RatePlanID] => 0:5:0:0
        )

    [ns2Rates] => SimpleXMLElement Object
        (
            [ns2Rate] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                            [ns2TPA_Extensions] => SimpleXMLElement
Object
                                (
                                    [ns3AccomSupplement] => Array
                                        (
                                            [0] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 109.59
                                                           
[AmountBeforeTax] => 109.59
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> false
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S0S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => *replaced*
                                                                )

                                                        )

                                                )

                                            [1] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 0.00
                                                           
[AmountBeforeTax] => 0.00
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> true
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S1S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => Child Extra Bed
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [ns2RoomRateDescription] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Name] => Buffet Breakfast
                )

        )

    [ns2Total] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [AmountAfterTax] => 86.52
                    [AmountBeforeTax] => 86.52
                    [CurrencyCode] => USD
                )

        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [RatePlanCode] => DEF
            [RatePlanID] => 0:5:0:0
        )

    [ns2Rates] => SimpleXMLElement Object
        (
            [ns2Rate] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                            [ns2TPA_Extensions] => SimpleXMLElement
Object
                                (
                                    [ns3AccomSupplement] => Array
                                        (
                                            [0] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 109.59
                                                           
[AmountBeforeTax] => 109.59
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> false
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S0S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => *replaced*
                                                                )

                                                        )

                                                )

                                            [1] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 0.00
                                                           
[AmountBeforeTax] => 0.00
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> true
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S1S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => Child Extra Bed
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [ns2RoomRateDescription] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Name] => Buffet Breakfast
                )

        )

    [ns2Total] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [AmountAfterTax] => 86.52
                    [AmountBeforeTax] => 86.52
                    [CurrencyCode] => USD
                )

        )

)

Actual result:
--------------
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [RatePlanCode] => DEF
            [RatePlanID] => 0:5:0:0
        )

    [ns2Rates] => SimpleXMLElement Object
        (
            [ns2Rate] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                            [ns2TPA_Extensions] => SimpleXMLElement
Object
                                (
                                    [ns3AccomSupplement] => Array
                                        (
                                            [0] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 109.59
                                                           
[AmountBeforeTax] => 109.59
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> false
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S0S
                                                        )

                                                    [0] =>
SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => *replaced*
                                                                )

                                                        )

                                                )

                                            [1] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 0.00
                                                           
[AmountBeforeTax] => 0.00
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> true
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S1S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => Child Extra Bed
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [ns2RoomRateDescription] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Name] => Buffet Breakfast
                )

        )

    [ns2Total] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [AmountAfterTax] => 86.52
                    [AmountBeforeTax] => 86.52
                    [CurrencyCode] => USD
                )

        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [RatePlanCode] => DEF
            [RatePlanID] => 0:5:0:0
        )

    [ns2Rates] => SimpleXMLElement Object
        (
            [ns2Rate] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                            [ns2TPA_Extensions] => SimpleXMLElement
Object
                                (
                                    [ns3AccomSupplement] => Array
                                        (
                                            [0] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 109.59
                                                           
[AmountBeforeTax] => 109.59
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> false
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S0S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => *replaced*
                                                                )

                                                        )

                                                )

                                            [1] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 0.00
                                                           
[AmountBeforeTax] => 0.00
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> true
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S1S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => Child Extra Bed
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [ns2RoomRateDescription] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Name] => Buffet Breakfast
                )

        )

    [ns2Total] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [AmountAfterTax] => 86.52
                    [AmountBeforeTax] => 86.52
                    [CurrencyCode] => USD
                )

        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [RatePlanCode] => DEF
            [RatePlanID] => 0:5:0:0
        )

    [ns2Rates] => SimpleXMLElement Object
        (
            [ns2Rate] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [UnitMultiplier] => 1
                                )

                            [ns2TPA_Extensions] => SimpleXMLElement
Object
                                (
                                    [ns3AccomSupplement] => Array
                                        (
                                            [0] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 109.59
                                                           
[AmountBeforeTax] => 109.59
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> false
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S0S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => *replaced*
                                                                )

                                                        )

                                                )

                                            [1] => SimpleXMLElement
Object
                                                (
                                                    [@attributes] =>
Array
                                                        (
                                                           
[AmountAfterTax] => 0.00
                                                           
[AmountBeforeTax] => 0.00
                                                           
[BestDiscountGrpNo] => 0
                                                            [Compulsory]
=> true
                                                           
[CurrencyCode] => USD
                                                            [ItemRPH] =>
0:5:0:0S1S
                                                        )

                                                   
[ns2SupplementDescription] => SimpleXMLElement Object
                                                        (
                                                           
[@attributes] => Array
                                                                (
                                                                   
[Name] => Child Extra Bed
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [ns2RoomRateDescription] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Name] => Buffet Breakfast
                )

        )

    [ns2Total] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [AmountAfterTax] => 86.52
                    [AmountBeforeTax] => 86.52
                    [CurrencyCode] => USD
                )

        )

)

-- 
Edit bug report at https://bugs.php.net/bug.php?id=70905&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=70905&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=70905&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=70905&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=70905&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=70905&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=70905&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=70905&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=70905&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=70905&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=70905&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=70905&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=70905&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=70905&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70905&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=70905&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=70905&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=70905&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70905&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=70905&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=70905&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#197212) next »