2020-04-22 17:18:31 1250次浏览 1条回答 0 悬赏 100 金钱

例如如下代码:

public function actionTest () { 
    return \Yii::createObject([ 
        'class' => 'yii\web\Response', 
        'format' => \yii\web\Response::FORMAT_XML, 
        'formatters' => [ 
            \yii\web\Response::FORMAT_XML => [ 
                'class' => 'yii\web\XmlResponseFormatter', 
                'rootTag' => 'urlset', //根节点 
                'itemTag' => 'url', //单元 
            ], 
        ], 
        'data' => [ //要输出的数据 
            [ 
                'loc' => 'http://********', 
            ], 
        ], 
    ]); 
}

但是我想输出的xml 结构如下,我该怎么配置我的formatters

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"       
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
  <url>
    <loc>http://www.example.org/business/article55.html</loc>
    <news:news>
    <news:publication>
      <news:name>The Example Times</news:name>
      <news:language>en</news:language>
    </news:publication>
    <news:publication_date>2008-12-23</news:publication_date>
      <news:title>Companies A, B in Merger Talks</news:title>
    </news:news>
  </url>
</urlset>

请问如何改写?

补充于 2020-04-24 17:18

另外请问 urlset中 xmlns属性该如何设置?
例如:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"       
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
  • 回答于 2020-04-24 17:10 举报

    news:news 中间不能有冒号,改写后如下

        return \Yii::createObject([
            'class' => 'yii\web\Response',
            'format' => \yii\web\Response::FORMAT_XML,
            'formatters' => [
                \yii\web\Response::FORMAT_XML => [
                    'class' => 'yii\web\XmlResponseFormatter',
                    'rootTag' => 'urlset', //根节点
                    'itemTag' => 'url', //单元
                ],
            ],
            'data' => [ //要输出的数据
                [
                    'loc' => 'http://********',
                    'newsnews' => [
                        'newsname' => 'The Example Times',
                    ]
                ],
            ],
        ]);
    
    1 条回复
    回复于 2020-04-24 17:18 回复

    这样输出的xml就不是我想要的效果了。

您需要登录后才可以回答。登录 | 立即注册
acking
总监

acking 上海

注册时间:2012-05-16
最后登录:2天前
在线时长:65小时44分
  • 粉丝13
  • 金钱2560
  • 威望0
  • 积分3210

热门问题