Skip to content

XmlSerializer configurable amount of spaces when $prettyPrint=true #303

Description

@llaville

Hello,

Because I used 4 spaces indentation (for my XML documents) rather than default 2 spaces provided by the DOMDocument, I wanted a solution to configure the XMLSerializer.

As we cannot alter indendation via DOMDocument

I propose two alternatives :

  1. the most simple and easy way (through a regex like suggested https://stackoverflow.com/questions/3325488/php-increase-indentation-of-domdocument-savexml)

  2. Change DOMDocument by XMLWriter that is able to configure it (for example : https://github.com/phar-io/manifest/blob/2.0.3/src/ManifestSerializer.php#L43-L44)

I've already tested solution 1 and it works as expected :

Patching:
https://github.com/CycloneDX/cyclonedx-php-library/blob/v2.1.2/src/Core/Serialization/XmlSerializer.php#L78

        $xml = $document->saveXML();
        $xml = \preg_replace('/^[ ]+(?=<)/m','$0$0', $xml);

Of course adding an option argument on class constructor to make it configurable at runtime.

What do you think ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions