本教程将手把手教你如何使用n8n的XML节点进行数据格式转换。无论是处理网站地图、对接遗留系统,还是为AI大模型准备结构化数据,这个教程都能让你快速上手。
XML节点是n8n中的一个核心数据处理工具,用于在JSON和XML两种常见的数据格式之间进行无缝转换。在现实工作中,你可能需要:
sitemap.xml 提取URL列表进行SEO分析XML节点就是解决这些场景的利器。
将结构化的JSON数据转换为标准的XML格式。这对于与只支持XML的老系统集成特别有用。
常用配置项:
| 参数 | 说明 | 默认值 |
|---|---|---|
| Root Name | 设置XML的根元素名称 | - |
| Headless | 是否省略XML声明头(<?xml version=...?>) | 关闭 |
| Cdata | 对文本节点使用CDATA包装,而不是转义特殊字符 | 关闭 |
| Allow Surrogate Chars | 是否允许Unicode替代字符 | 关闭 |
从XML文件或数据中提取有用的信息,转换为易于处理的JSON格式。这是处理网站地图、API响应的常用方法。
常用配置项:
| 参数 | 说明 | 示例 |
|---|---|---|
| Explicit Array | 始终将子节点放入数组(开启),或仅多个子节点时才使用数组(关闭) | 关闭为更紧凑的结构 |
| Explicit Root | 是否在结果中保留根节点 | 通常关闭 |
| Ignore Attributes | 忽略所有XML属性,仅创建文本节点 | 关闭以保留属性 |
| Merge Attributes | 是否将属性与子元素合并为父级属性 | 开启 |
| Normalize Tags | 是否将所有标签名转换为小写 | 开启 |
| Trim | 是否移除文本节点的首尾空格 | 开启 |

所有模式都支持的两个重要参数:
$<tag attr="value"> 会变成 {"$attr": "value", "_": "内容"}_"_" 键下这是XML节点最常见的应用场景——将网站地图转换为JSON,用于SEO分析或URL爬取。
第一步:添加HTTP请求节点
在n8n工作流中:
https://example.com/sitemap.xml运行节点后,你会获得原始的XML数据。
第二步:添加XML节点进行转换
XML to JSONbody(因为HTTP请求返回的XML在body字段中)配置完成后,XML将自动转换为结构化的JSON对象。
第三步:提取URL列表
网站地图通常有这样的结构:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/page1</loc>
<lastmod>2024-01-15</lastmod>
</url>
<url>
<loc>https://example.com/page2</loc>
<lastmod>2024-01-20</lastmod>
</url>
</urlset>转换后的JSON将是:
{
"urlset": {
"url": [
{
"loc": "https://example.com/page1",
"lastmod": "2024-01-15"
},
{
"loc": "https://example.com/page2",
"lastmod": "2024-01-20"
}
]
}
}添加一个Split Out节点来分离每个URL元素,便于进一步处理。
很多开发者发现,为AI大模型提供XML格式的结构化指令效果更好。这就是JSON转XML的应用场景。
第一步:准备JSON数据
使用"Set"节点创建你的数据:
{
"agent": {
"name": "ContentAnalyzer",
"description": "分析并总结文章内容",
"instructions": "请提取标题、摘要和关键词"
},
"input_format": "markdown",
"output_format": "json"
}第二步:添加XML节点转换
JSON to XMLbody)agent_config(作为XML的根元素)第三步:获得XML输出
转换后的XML将是:
<?xml version="1.0"?>
<agent_config>
<agent>
<name>ContentAnalyzer</name>
<description>分析并总结文章内容</description>
<instructions>请提取标题、摘要和关键词</instructions>
</agent>
<input_format>markdown</input_format>
<output_format>json</output_format>
</agent_config>此XML格式的结构化指令可以直接传递给OpenAI、Claude等LLM API。
当XML包含属性(attributes)时,如何正确处理?
原始XML示例:
<document version="1.0" language="zh">
<title author="张三">教程标题</title>
<content>教程内容</content>
</document>配置XML节点(XML to JSON):
$_得到的JSON:
{
"document": {
"$version": "1.0",
"$language": "zh",
"title": {
"$author": "张三",
"_": "教程标题"
},
"content": "教程内容"
}
}| 问题 | 原因 | 解决方案 |
|---|---|---|
| 错误:"Item has no JSON property" | XML数据在错误的属性字段中 | 检查"Property Name"配置是否匹配数据源 |
| 转换后的JSON单层结构 | 未配置"Explicit Array" | 根据需求开启或关闭此选项 |
| 特殊字符显示乱码 | 未使用CDATA包装 | JSON转XML时开启"Cdata"选项 |
| 性能变慢 | 处理过大的XML文件 | 使用Split节点分批处理,或在HTTP层面添加分页 |
| 属性没有被识别 | "Ignore Attributes"被开启了 | 关闭此选项以保留XML属性 |
这个工作流自动获取网站地图、解析所有URL,并检查特定页面是否更新。
{
"meta": {
"instanceId": "workflow-sitemap-analysis",
"templateCredsSetupCompleted": false
},
"name": "Sitemap XML to JSON Analysis",
"nodes": [
{
"parameters": {
"rule": {
"interval": [
{
"triggerAtHour": 9
}
]
}
},
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1.2,
"position": [
-600,
-200
],
"id": "schedule_trigger",
"name": "Daily Schedule at 9 AM"
},
{
"parameters": {
"method": "GET",
"url": "{{ $env.SITEMAP_URL || 'https://example.com/sitemap.xml' }}",
"sendHeaders": false,
"responseFormat": "text"
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-300,
-200
],
"id": "fetch_sitemap",
"name": "Fetch Sitemap"
},
{
"parameters": {
"mode": "xmlToJson",
"propertyName": "body",
"options": {
"explicit_root": false,
"normalize_tags": true,
"trim": true,
"merge_attributes": true
}
},
"type": "n8n-nodes-base.xml",
"typeVersion": 1,
"position": [
50,
-200
],
"id": "xml_converter",
"name": "Convert XML to JSON"
},
{
"parameters": {
"fieldToSplitOut": "url",
"appendFields": ""
},
"type": "n8n-nodes-base.splitOut",
"typeVersion": 1,
"position": [
350,
-200
],
"id": "split_urls",
"name": "Split URLs"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "url_field",
"name": "page_url",
"type": "string",
"value": "={{ $json.loc }}"
},
{
"id": "lastmod_field",
"name": "last_modified",
"type": "string",
"value": "={{ $json.lastmod }}"
}
]
}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
650,
-200
],
"id": "set_output",
"name": "Format Output"
}
],
"connections": {
"schedule_trigger": {
"main": [
[
{
"node": "fetch_sitemap",
"type": "main",
"index": 0
}
]
]
},
"fetch_sitemap": {
"main": [
[
{
"node": "xml_converter",
"type": "main",
"index": 0
}
]
]
},
"xml_converter": {
"main": [
[
{
"node": "split_urls",
"type": "main",
"index": 0
}
]
]
},
"split_urls": {
"main": [
[
{
"node": "set_output",
"type": "main",
"index": 0
}
]
]
}
}
}工作流说明:
实际应用:
你可以在最后添加:
SITEMAP_URL环境变量或直接修改URL[1] 官方文档: https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.xml/
[2] n8n系列教程: https://www.undsky.com/blog/?category=n8n%E6%95%99%E7%A8%8B#