Skip to content

simplexml_load_string does not work out of the box #267

Open
@nreynis

Description

@nreynis

simplexml_load_string will not return false, but will emits errors unless you are setting libxml_use_internal_errors(true); before.

I am not sure this is necessarly something you want to handle. 🤔

To me that kind of looks like an encapsulation issue: I expect the library to get care of those details and provide me an exception.

Instead I've got to do all that by myself:

        try {
            libxml_use_internal_errors(true);
            $result = \Safe\simplexml_load_string($data, $class_name, $options, $namespace_or_prefix, $is_prefix);
            libxml_clear_errors();
            return $result;
        } catch (SimplexmlException $e) {
            $messages = [];
            foreach (libxml_get_errors() as $error) {
                $messages[] = $error->message;
            }
            libxml_clear_errors();
            $message = 'Impossible to process the provided XML: '.join(', ', $messages);
            throw new SimplexmlException($message, $e);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions