Q
Q
Quadro2012-08-16 11:59:14
PHP
Quadro, 2012-08-16 11:59:14

xml serializer for php

Tell me if there are normal xml serializers for php like XML_Serializer from pear. It is good for everything, except for performance. It is convenient to set up, it is convenient to overtake array-> xml, but for the api, the output of a relatively large xml (1.5 mb) was needed and the serialization already took 3-4 seconds. json_encode - an order of magnitude smaller, it is clear that it is built into php, and therefore faster, but still.
What do you use in your projects, self-written?

What is the convenience of Pear XML_Serializer:

require_once 'XML/Serializer.php';

$options = array(
                    XML_SERIALIZER_OPTION_INDENT               => '    ',
                    XML_SERIALIZER_OPTION_LINEBREAKS           => "\n",
                    XML_SERIALIZER_OPTION_DEFAULT_TAG          => 'unnamedItem',
                    XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => false,
                    XML_SERIALIZER_OPTION_ATTRIBUTES_KEY       => '_attributes',
                    XML_SERIALIZER_OPTION_CONTENT_KEY          => '_content'
                );

$data = array(
                'foo' => array(
                                '_attributes' => array( 'version' => '1.0', 'foo' => 'bar' ),
                                //'_content'    => 'test & test',
                                'places' => array("v1" => 1, 'v2' => 3)
                              ),
                'schst' => 'Stephan Schmidt'
            );

$serializer = new XML_Serializer($options);

$result = $serializer->serialize($data);

if ($result === true) {
    $xml = $serializer->getSerializedData();
    echo '<pre>';
    echo htmlspecialchars($xml);
    echo '</pre>';
} else {
    echo '<pre>';
    print_r($result);
    echo '</pre>';
}


we get
<array>
    <foo foo="bar" version="1.0">
        <places>
            <v1>1</v1>
            <v2>3</v2>
        </places>
    </foo>
    <schst>Stephan Schmidt</schst>
</array>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
winbackgo, 2012-08-16
@winbackgo

I think it will be faster if you use simple_xml. Here's how snipplr.com/view/3491/ is an example . I don't know for what purposes you need it, but IMHO json is much easier and better to use.

M
MARDEN, 2012-08-17
@MARDEN

Try the Serializer component from the Symfony framework.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question