AaronDDM / XMLBuilder

A simple PHP XML builder library written for PHP 7.2+
MIT License
28 stars 4 forks source link

Unexpected closing tags #16

Open hashhesh opened 2 years ago

hashhesh commented 2 years ago

Hi @AaronDDM thanks for this lib it's more connivant. Could you please help I have came across with really weird closing tag accidently even i didn't understood how/why
So this is my code

    $xmlWriterService = new XMLWriterService();
    $xml_builder_obj = new XMLBuilder($xmlWriterService);
    $root = $xml_builder_obj->createXMLArray()->start('ProductData');

    $root->loop(
        function (XMLArray $XMLArray) use ($results, $relatives_tags, $product_type_attributes) {

            foreach ($product_type_attributes as $tag_index => $tag_value) {
                if (is_array($tag_value) && fn_he_amazon_array_depth($tag_value) === 1) {

                    $groups = $relatives_tags(key($tag_value));

                    $XMLArray->add(key($tag_value), $results[$groups[1]], [explode(',',$groups[2])[2] => $results[$groups[2]]]);

                } else {
                    $XMLArray->add($tag_index, $tag_value);
                }
            }
        });
    $root->end();

and this is the outcomes

<?xml version="1.0" encoding="UTF-8"?>
<ProductData>
    <Viscosity>2T</Viscosity>
    <BatteryAverageLife unitOfMeasure="days">2</BatteryAverageLife>
</ProductData>
<ColorMap>beige</ColorMap>
<SizeMap>XX-Small</SizeMap>
<CompatibleWithVehicleType>Airplane</CompatibleWithVehicleType>

while the expected output is like below:

<?xml version="1.0" encoding="UTF-8"?>
<ProductData>
        <Viscosity>2T</Viscosity>
        <BatteryAverageLife unitOfMeasure="days">2</BatteryAverageLife>
        <ColorMap>beige</ColorMap>
        <SizeMap>XX-Small</SizeMap>
       <CompatibleWithVehicleType>Airplane</CompatibleWithVehicleType>
</ProductData>

ProductData should be closed at the end not to be closed after the second tag. I have used startLoop and using $root instead of XMLArray but the same result.

So, Could you please help to understand this issue? though if I comment the first if statement it's works fine.

Thanks in advance

hashhesh commented 2 years ago

Hi, just to update, after change to SabreXMLWriterService it works fine but honestly I still don't know why?!!!!!

AaronDDM commented 2 years ago

Hey @hashhesh , that is indeed strange! Thanks for reporting this. I’ll check it out and get back to you - you might have discovered a bug!

I’m glad you have a workaround at least.

hashhesh commented 2 years ago

hello @AaronDDM , highly appreciated your swift reply. I'm really curious to know what's the issue too.

Thanks again Aaron.