如何在Symfony XmlEncoder中设置编码UTF-8和xmlns属性

弗拉基米尔·莫诺玛xxx

我使用Symfony 3序列化器,并通过XmlEncoder将对象转换为XML。但是XmlEncoder在xml prolog中没有编码类型。如何解决这个问题呢?

是否可以在根元素之后添加带有参数的自定义属性?

有什么办法在根元素中设置xmlns吗?

我需要这样的XML输出:

<?xml version="1.0" encoding="utf-8"?>
<realty-feed xmlns="http://webmaster.yandex.ru/schemas/feed/realty/2010-06">
<generation-date>2010-10-05T16:36:00+04:00</generation-date> 
...
</realty-feed>

现在我明白了:

<?xml version="1.0"?>
<realty-feed>
...
</realty-feed>

我的序列化器代码片段:

$xmlEncoder = new XmlEncoder('realty-feed');
$normalizer = new CustomNormalizer();
$serializer = new Serializer(array($normalizer),array($xmlEncoder));
$output = $serializer->serialize($objectData, 'xml');
弗拉基米尔·莫诺玛xxx

我解决了我的问题。我创建了自己的类,并编写了与默认类XmlEncoder中相同的方法,但对一些标准的公共和私有方法进行了一些更改。

    class N1XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
    {
        ...

        /**
         * {@inheritdoc}
         */
        public function encode($data, $format, array $context = array())
        {
            if ($data instanceof \DOMDocument) {
                return $data->saveXML();
            }

            $xmlRootNodeName = $this->resolveXmlRootName($context);

            $this->dom = $this->createDomDocument($context);
            $this->format = $format;
            $this->context = $context;

            if (null !== $data && !is_scalar($data)) {
                $root = $this->dom->createElementNS('http://webmaster.yandex.ru/schemas/feed/realty/2010-06', $xmlRootNodeName);
                $dateTime = new \DateTime('now');
                $dateTimeStr = $dateTime->format(DATE_ATOM);
                $rootDate = $this->dom->createElement('generation-date', $dateTimeStr);
                $this->dom->appendChild($root);
                $root->appendChild($rootDate);
                $this->buildXml($root, $data, $xmlRootNodeName);
            } else {
                $this->appendNode($this->dom, $data, $xmlRootNodeName);
            }

            return $this->dom->saveXML();
        }
...
private function createDomDocument(array $context)
    {
        $document = new \DOMDocument('1.0','utf-8');

        // Set an attribute on the DOM document specifying, as part of the XML declaration,
        $xmlOptions = array(
            // nicely formats output with indentation and extra space
            'xml_format_output' => 'formatOutput',
            // the version number of the document
            'xml_version' => 'xmlVersion',
            // the encoding of the document
            'xml_encoding' => 'encoding',
            // whether the document is standalone
            'xml_standalone' => 'xmlStandalone',
        );
        foreach ($xmlOptions as $xmlOption => $documentProperty) {
            if (isset($context[$xmlOption])) {
                $document->$documentProperty = $context[$xmlOption];
            }
        }

        return $document;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Jenkins中将编码设置为UTF-8

Hibernate + MySQL:如何为数据库和表设置编码utf-8

如何在Spring托管的MySQL JDBC连接上设置useUnicode = true和characterEncoding = utf8属性

如何更改Castor映射以从XML输出的元素中删除“ xmlns:xsi”和“ xsi:type”属性?

如何在Go中从编码转换为UTF-8?

如何在golang中以UTF-8编码gob?

如何更好地将JVM编码属性设置为UTF-8

JSP / GlassFish:如何正确设置UTF-8编码

如何从xml和Java中的根元素中删除xmlns属性

如何在Python中修复损坏的utf-8编码?

如何在Python中编码(utf8mb4)

Ruby,Nokogiri:如何在整个nokogiri解析,erb模板和HTML文件编码中确保UTF8

如何使用jdbc和MySQL正确设置utf8编码?

如何在R中读取utf-8编码的文本

如何在RStudio中检测非UTF-8编码

如何在Python中默认设置PYTHONUTF8环境变量以启用UTF-8编码?

如何在Symfony 5中编码密码?

如何在我的php联系人表单中设置UTF-8编码。仅拉丁字符正确显示

如何处理libpq中的UTF-8编码错误?

如何设置XNode对象的xmlns属性

如何在Firefox中将备用编码设置为UTF-8?

如何在php文件中设置utf-8字符编码

如何在Python SOAPpy中设置UTF-8

如何在UTF-8中为Android中的不同字符集进行编码

如何在 Symfony 4 中通过 GET 请求访问属性?

如何在 Symfony 4 中制作喜欢和不喜欢?

如何在 python 中以 UTF-8 样式编写和编码我的文件?

如何在改造android中设置utf-8编码?

如何正确编码 UTF-8 中的 HTTP 响应字节?