如何解决使用 Symfony 序列化程序构建和自定义 XML 文件
我正在研究 Symfony 序列化程序,并且我正在尝试根据对我的 Mongo 数据库进行的查询结果生成 XML。但我想要一个具有非常具体结构的 XML。 我可能可以通过自定义 XmlEncoder 来做到这一点,但它可能比必要的更复杂。
我知道我可以在不编辑编码器类的情况下更改默认主标签
这是我的控制器的代码:
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 举报,一经查实,本站将立刻删除。