You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power Automate遍历XML转JSON数据失败问题排查

问题:处理美国国务院RSS Feed时Power Automate遍历出错

需求说明

  • 将美国国务院RSS Feed转换为数组
  • 按日期过滤,移除指定日期及之前的数据
  • 更新国家标签(例如把A3改为44)
  • 将处理后的数据保存到Excel,仅保留国家名称、威胁等级、国家标签列

当前流程与报错

我现在的Power Automate流程步骤:

  1. 将XML上传至OneDrive
  2. 转换为JSON并解析,生成的Schema如下:
{
    "type": "object",
    "properties": {
        "?xml": {
            "type": "object",
            "properties": {
                "@@version": {
                    "type": "string"
                },
                "@@encoding": {
                    "type": "string"
                }
            }
        },
        "rss": {
            "type": "object",
            "properties": {
                "@@xmlns:dc": {
                    "type": "string"
                },
                "@@version": {
                    "type": "string"
                },
                "channel": {
                    "type": "object",
                    "properties": {
                        "title": {
                            "type": "string"
                        },
                        "link": {
                            "type": "string"
                        },
                        "description": {
                            "type": "string"
                        },
                        "item": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "title": {
                                        "type": "string"
                                    },
                                    "pubDate": {
                                        "type": "string"
                                    },
                                    "link": {
                                        "type": "string"
                                    },
                                    "guid": {
                                        "type": "string"
                                    },
                                    "category": {
                                        "type": "array",
                                        "items": {
                                            "type": "object",
                                            "properties": {
                                                "@@domain": {
                                                    "type": "string"
                                                },
                                                "#text": {
                                                    "type": "string"
                                                }
                                            },
                                            "required": [
                                                "@@domain",
                                                "#text"
                                            ]
                                        }
                                    },
                                    "dc:identifier": {
                                        "type": "string"
                                    },
                                    "description": {
                                        "type": "object",
                                        "properties": {
                                            "#cdata-section": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                },
                                "required": [
                                    "title",
                                    "pubDate",
                                    "link",
                                    "guid",
                                    "category",
                                    "dc:identifier",
                                    "description"
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}
  1. 解析后,用Apply to each遍历@{body('Parse_JSON')?['rss']?['channel']?['item']},但在循环内嵌套了另一个Apply to each处理@{items('Apply_to_each_2')?['pubDate']},并尝试用{ "pd": @{items('Apply_to_each_2')?['pubDate']}}追加到数组变量,结果报错:

The execution of template action 'Apply_to_each' failed: the result of the evaluation of 'foreach' expression '@items('Apply_to_each_2')?['pubDate']' is of type 'String'. The result must be a valid array.

错误原因与修正方案

错误原因

从Schema能看到pubDate是单个字符串类型,而你给嵌套的Apply to each传入了单个字符串——但Apply to each要求遍历的必须是数组类型,直接传字符串自然会触发错误。

修正步骤

  1. 删掉嵌套的Apply to each:不需要遍历单个pubDate,直接在顶层的Apply to each(遍历item数组)里处理当前item的pubDate就行。
  2. 添加日期过滤逻辑:在顶层循环内加Condition动作,把items('Apply_to_each')?['pubDate']转换成日期格式后,和指定日期对比(比如用formatDateTime(items('Apply_to_each')?['pubDate'], 'yyyy-MM-dd')转格式,再判断是否大于指定日期)。
  3. 处理国家标签:遍历当前item的category数组(Schema里category是数组),按@@domain区分标签类型,用Replace或Switch动作替换标签值(比如把"A3"换成"44")。
  4. 构建目标数据:在符合日期条件的分支里,构建包含国家名称(从title或category提取)、威胁等级、转换后标签的对象,追加到数组变量。
  5. 写入Excel:循环结束后,把数组变量写入Excel表格,对应好列映射即可。

优化建议

  • 直接用HTTP动作获取RSS Feed,不用先传OneDrive,减少流程步骤。
  • RSS的pubDate一般是EEE, dd MMM yyyy HH:mm:ss zzz格式,Power Automate的formatDateTime可以直接识别,转换时不用额外处理格式。

内容的提问来源于stack exchange,提问作者Wesley Young

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 13:10:09