使用 Symfony 序列化程序构建和自定义 XML 文件

问题描述

我正在研究 Symfony 序列化程序,并且我正在尝试根据对我的 Mongo 数据库进行的查询结果生成 XML。但我想要一个具有非常具体结构的 XML。 我可能可以通过自定义 XmlEncoder 来做到这一点,但它可能比必要的更复杂。

我知道我可以在不编辑编码器类的情况下更改认主标签 ,只需使用参数,但我还没有找到如何自定义 XML 文件的其余部分,我可以用同样的方式来做吗?我想答案是肯定的,但我没有找到方法

这是我的控制器的代码

public function servicesXml (ContentRepository $content): Response
  {
    $encoder = [new XmlEncoder ('result')];
    $normalizer = [new Objectnormalizer ()];
    $serializer = new Serializer ($normalizer,$encoder);

    $response = $content-> getContents ();
$tag = [
      '@xmlns' => 'http://www.w3.org/2005/Atom','#' => $response
    ];
    $response = $serializer-> serialize ($response,'xml',[
  'xml_format_output' => true,'xml_encoding' => 'utf-8','xml_root_node_name' => 'Feed']);

    return new Response ($response);
  }

认使用 XmlEncoder,我有类似的东西:

<response>
    <item key="0">
        <id>1</id>
        <header>
            <title>Title</title>
            <subtitle>Subtitle</subtitle>
            <module>
                <date>
                    <time>-292277022657-01-27T08:29:52+0000</time>
                </date>
            </module>
            <author>
                <name>Name</name>
            </author>
        </header>
        <body>
                <html><![CDATA[<p>Text</p>]]></html>
            </body>
            <location>
                    <country_code>??</country_code>
                    <latitude>0</latitude>
                    <longitude>0</longitude>
                </location>
    </item>
</response>

我希望有:

<Feed xlmns="http://www.w3.org/2005/Atom">
    <logo>URL</logo>
    <entry>
        <id>
        <title>Title</title>
        <notsubtitle>Subtitle</notsubtitle>
        <created>-292277022657-01-27T08:29:52+0000</created>
        <author>
            <name>Name</name>
        </author>
        <content type="xhtml">
            <div><p>Text</p></div>
        </content>
    </entry>
</Feed>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)