Sabre XML writer的输出是一个普通的字符串

时间:2018-05-21 07:43:14

标签: php xml

我使用的是Sabre XML编写器库。

我正在尝试网站本身的例子:

 $writer = new Sabre\Xml\Writer();
        $writer->openMemory();
        $writer->namespaceMap = [
            'http://example.org/' => 'e',
        ];

        $writer->startElement('{http://example.org/}root');
        $writer->write([
    '{http://example.org/ns}title' => 'Foundation',
    '{http://example.org/ns}author' => 'Isaac Asimov',]);

        $writer->endElement();
        echo $writer->outputMemory();

我期待这个输出:

<?xml version="1.0">
<e:root>
    <e:title>Foundation</e:title>
    <e:author>Isaac Asimov</e:author>
</e:root>

但我得到了一个简单的字符串:

FoundationIsaac Asimov

我已经尝试了XML阅读器功能,它们工作正常,因此正确地编写了包。

任何帮助?

1 个答案:

答案 0 :(得分:1)

我不得不使用标题:

header('Content-Type: text/xml');

现在它完美无缺。