Node-RED XML ノード ガイド

☁️ クラウドOK 環境とは?

目次

1. 概要

XML ノードは、XML文字列とJavaScriptオブジェクトを相互変換するノードです。XML形式のデータを扱うAPIやファイルとの連携に使用します。

翻訳者に例えると:XML ノードは2つの言語(XML形式とJavaScriptオブジェクト形式)を相互に翻訳する通訳者のようなものです。どちらの形式でデータを受け取っても、もう一方の形式に変換してくれます。

XML文字列 → XML → オブジェクト

ポイント:

動作を図で見る

xml ノードは、XML 形式の文字列とオブジェクトを相互に変換します。

入ってくる msgpayload:'<temp unit="C">25</temp>'xml出ていく msgpayload: { temp: { $: {unit: "C"}, _: "25" }}属性は $、文字は _ に入る
属性は $、要素の文字は _ という名前のプロパティに入る

2. 設定詳細

XML ノードの設定はごく少ない。指定したプロパティの値を見て、文字列なら XML をオブジェクトへ、オブジェクトなら XML へと、変換の向きを自動で切り替える。設定項目は「どのデータを変換するか」と「XML をオブジェクトにするときの表現」の 2 つに分かれる。

基本

設定項目説明既定値
プロパティ変換の対象にする msg のプロパティ。この値が文字列なら XML からオブジェクトへ、オブジェクトなら XML へ変換する。msg.payload
名前エディター上の表示名。動作には影響しない。(空)

XMLからオブジェクトへ変換

次の 2 つは、入力が XML 文字列のとき(XML からオブジェクトへ変換するとき)にだけ働く。XML の属性と要素のテキストを、オブジェクトのどのキーに入れるかを決める。逆向き(オブジェクトから XML)では使われない。

設定項目説明既定値
XMLタグ属性を次のプロパティ名として表現要素の属性(<book id="1"> の id)をまとめて格納する子オブジェクトのキー名。既定の $ なら { "book": { "$": { "id": "1" } } } のように属性が $ の下に入る。$(空欄時)
文字列を参照するための接頭辞属性や子要素を持つ要素について、その要素自身のテキストを格納するキー名。既定の _ なら <tag a="1">text</tag> が { "$": { "a": "1" }, "_": "text" } になる。_(空欄時)

補足(上級者向け): 変換の細かな挙動は msg.options に xml2js のオプションを渡して調整できる。属性とテキストのキー名はエディターの設定が優先され、空欄のときに msg.options.attrkey / msg.options.charkey や既定値が使われる。オブジェクトから XML を組み立てるときは msg.options.renderOpts(整形など)が反映される。

3. 変換ルール

XML → オブジェクト変換

入力(XML)

<person id="1"> <name>田中太郎</name> <age>30</age> </person>

出力(オブジェクト)

{ "person": { "$": { "id": "1" }, "name": ["田中太郎"], "age": ["30"] } }

オブジェクト → XML変換

入力(オブジェクト)

{ "book": { "$": { "isbn": "123" }, "title": "Node-RED入門", "price": 2000 } }

出力(XML)

<?xml version="1.0"?> <book isbn="123"> <title>Node-RED入門</title> <price>2000</price> </book>

属性とテキストの表現

XML要素オブジェクト表現
属性 <tag attr="value">{ "$": { "attr": "value" } }
テキスト <tag>text</tag>{ "tag": ["text"] } または { "_": "text" }
属性+テキスト{ "$": { "attr": "v" }, "_": "text" }

4. 実用的な使用パターン

サンプルフローのインポート方法:

  1. 下のフローJSON(公式サンプルフロー、または応用フロー)をコピー
  2. Node-REDエディタで メニュー → 読み込み を選択
  3. JSONをペーストして「読み込み」をクリック

公式サンプルフロー

最初のサンプルフローは、Node-RED本体に同梱されている公式サンプル3つ(「Convert JavaScript object to XML」「Convert XML to JavaScript object」「Control conversion using options property」)を1つのタブにまとめ、名前の無かった inject ノードと debug ノードに日本語の名前を付けたものです。エディタの メニュー → 読み込み → サンプル → node-red → parser → xml からも、元のフローを開けます。

📥 公式サンプルフローJSON(クリックで展開)
[ { "id": "3aeea029449df3be", "type": "tab", "label": "xml", "disabled": false, "info": "", "env": [] }, { "id": "82f1bd0b.43474", "type": "xml", "z": "3aeea029449df3be", "name": "", "property": "payload", "attr": "", "chr": "", "x": 530, "y": 180, "wires": [ [ "1cd4ad02.9a5423" ] ] }, { "id": "84222b92.d65d18", "type": "inject", "z": "3aeea029449df3be", "name": "例1: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 200, "y": 180, "wires": [ [ "cdd1c154.3a655" ] ] }, { "id": "7b014430.dfd94c", "type": "comment", "z": "3aeea029449df3be", "name": "Convert JavaScript object to XML", "info": "XML node can convert JavaScript object to XML string.", "x": 240, "y": 120, "wires": [] }, { "id": "1cd4ad02.9a5423", "type": "debug", "z": "3aeea029449df3be", "name": "例1: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 690, "y": 180, "wires": [] }, { "id": "cdd1c154.3a655", "type": "template", "z": "3aeea029449df3be", "name": "JS object", "field": "payload", "fieldType": "msg", "format": "json", "syntax": "plain", "template": "{\n \"kind\": \"Apple\",\n \"price\": 100,\n \"origin\": \"Canada\"\n}", "output": "json", "x": 360, "y": 180, "wires": [ [ "82f1bd0b.43474" ] ] }, { "id": "93e423a9.a407d", "type": "xml", "z": "3aeea029449df3be", "name": "", "property": "payload", "attr": "", "chr": "", "x": 530, "y": 360, "wires": [ [ "2d0dde7e.a50082" ] ] }, { "id": "ba1dab90.8d1da8", "type": "inject", "z": "3aeea029449df3be", "name": "例2: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 200, "y": 360, "wires": [ [ "16617f26.14ced1" ] ] }, { "id": "a9f97b00.57d658", "type": "comment", "z": "3aeea029449df3be", "name": "Convert XML to JavaScript object", "info": "XML node can convert XML string to JavaScript object.", "x": 240, "y": 300, "wires": [] }, { "id": "2d0dde7e.a50082", "type": "debug", "z": "3aeea029449df3be", "name": "例2: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 690, "y": 360, "wires": [] }, { "id": "16617f26.14ced1", "type": "template", "z": "3aeea029449df3be", "name": "XML string", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<fruit id=\"100\">\n <kind>Apple</kind>\n <price>100</price>\n <origin>Canada</origin>\n</fruit>", "output": "str", "x": 370, "y": 360, "wires": [ [ "93e423a9.a407d" ] ] }, { "id": "581bd648.636628", "type": "xml", "z": "3aeea029449df3be", "name": "", "property": "payload", "attr": "", "chr": "", "x": 710, "y": 540, "wires": [ [ "b74237dc.1e5028" ] ] }, { "id": "d0899f9b.f1ac6", "type": "inject", "z": "3aeea029449df3be", "name": "例3: 実行", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 200, "y": 540, "wires": [ [ "f04ffb9a.68edb8" ] ] }, { "id": "8a214c05.dc61f", "type": "comment", "z": "3aeea029449df3be", "name": "Control conversion using options property", "info": "XML node can control conversion by setting `options` property (defined by [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/README.md#options)) in input message.", "x": 260, "y": 480, "wires": [] }, { "id": "b74237dc.1e5028", "type": "debug", "z": "3aeea029449df3be", "name": "例3: 結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "false", "statusVal": "", "statusType": "auto", "x": 870, "y": 540, "wires": [] }, { "id": "f04ffb9a.68edb8", "type": "template", "z": "3aeea029449df3be", "name": "XML string", "field": "payload", "fieldType": "msg", "format": "html", "syntax": "plain", "template": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<fruit id=\"100\">\n <kind>Apple</kind>\n <price>100</price>\n <origin>Canada</origin>\n</fruit>", "output": "str", "x": 370, "y": 540, "wires": [ [ "fedf79.5889c088" ] ] }, { "id": "fedf79.5889c088", "type": "change", "z": "3aeea029449df3be", "name": "set options", "rules": [ { "t": "set", "p": "options", "pt": "msg", "to": "{\"explicitArray\":false}", "tot": "json" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 550, "y": 540, "wires": [ [ "581bd648.636628" ] ] } ]

フローの読み解き

フローには、公式サンプルの3つの例が、上から順に並んでいます。それぞれの先頭に、英語の comment ノードがあります。どの例も、「例N: 実行」(inject)のボタンで動かし、「例N: 結果」(debug)に結果が出ます。inject が送る値(時刻)は使われません。変換するデータは、次の template ノードに書いてあります。

例 comment ノードの見出し(意味) ノードの並びと設定 「例N: 結果」に出るもの
例1 Convert JavaScript object to XML(JavaScript のオブジェクトを XML に変換する) 「例1: 実行」→「JS object」(template)→ xml →「例1: 結果」。template は「出力形式」が「JSON」で、書いてある JSON をオブジェクトにして msg.payload に入れる <?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><kind>Apple</kind><price>100</price><origin>Canada</origin></root>
例2 Convert XML to JavaScript object(XML を JavaScript のオブジェクトに変換する) 「例2: 実行」→「XML string」(template)→ xml →「例2: 結果」。template は「出力形式」が「平文」で、XML の文字列をそのまま msg.payload に入れる {"fruit":{"$":{"id":"100"},"kind":["Apple"],"price":["100"],"origin":["Canada"]}}
例3 Control conversion using options property(options プロパティで変換を調整する) 「例3: 実行」→「XML string」(template)→「set options」(change)→ xml →「例3: 結果」。XML は例2と同じ。change が msg.options に {"explicitArray":false} を入れる {"fruit":{"$":{"id":"100"},"kind":"Apple","price":"100","origin":"Canada"}}

3つの xml ノードは、どれも設定が既定のままです。xml ノードは、届いた msg.payload がオブジェクトなら XML の文字列に、文字列ならオブジェクトに変換します。結果を見比べて、次の点を確かめてください。

応用(サイト独自): 属性のある XML と、値の取り出し

ここから先は公式サンプルではなく、本サイトが用意した応用教材です。属性や入れ子のある XML を変換する部分と、変換した結果から値を取り出す部分があり、下のパターン1〜3の実例になっています。

📥 応用フローJSON(クリックで展開)
[ { "id": "xml_sample_tab", "type": "tab", "label": "XML サンプル", "disabled": false, "info": "" }, { "id": "xml_comment1", "type": "comment", "z": "xml_sample_tab", "name": "━━━ XML → オブジェクト変換 ━━━", "info": "", "x": 180, "y": 40, "wires": [] }, { "id": "xml_inject1", "type": "inject", "z": "xml_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["xml_template1"]] }, { "id": "xml_template1", "type": "template", "z": "xml_sample_tab", "name": "XML文字列", "field": "payload", "fieldType": "msg", "format": "text", "syntax": "plain", "template": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<catalog>\n <book id=\"001\">\n <title>Node-RED入門</title>\n <author>山田太郎</author>\n <price currency=\"JPY\">2500</price>\n </book>\n <book id=\"002\">\n <title>IoT実践ガイド</title>\n <author>鈴木花子</author>\n <price currency=\"JPY\">3000</price>\n </book>\n</catalog>", "output": "str", "x": 290, "y": 100, "wires": [["xml_parse"]] }, { "id": "xml_parse", "type": "xml", "z": "xml_sample_tab", "name": "XML→Obj", "property": "payload", "attr": "", "chr": "", "x": 450, "y": 100, "wires": [["xml_debug1"]] }, { "id": "xml_debug1", "type": "debug", "z": "xml_sample_tab", "name": "オブジェクト出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 620, "y": 100, "wires": [] }, { "id": "xml_comment2", "type": "comment", "z": "xml_sample_tab", "name": "━━━ オブジェクト → XML変換 ━━━", "info": "", "x": 180, "y": 180, "wires": [] }, { "id": "xml_inject2", "type": "inject", "z": "xml_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 240, "wires": [["xml_func1"]] }, { "id": "xml_func1", "type": "function", "z": "xml_sample_tab", "name": "オブジェクト生成", "func": "msg.payload = {\n \"order\": {\n \"$\": { \"id\": \"ORD-001\" },\n \"customer\": \"田中商店\",\n \"items\": {\n \"item\": [\n { \"name\": \"商品A\", \"qty\": \"2\" },\n { \"name\": \"商品B\", \"qty\": \"1\" }\n ]\n },\n \"total\": \"5000\"\n }\n};\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 300, "y": 240, "wires": [["xml_build"]] }, { "id": "xml_build", "type": "xml", "z": "xml_sample_tab", "name": "Obj→XML", "property": "payload", "attr": "", "chr": "", "x": 470, "y": 240, "wires": [["xml_debug2"]] }, { "id": "xml_debug2", "type": "debug", "z": "xml_sample_tab", "name": "XML出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 620, "y": 240, "wires": [] }, { "id": "xml_comment3", "type": "comment", "z": "xml_sample_tab", "name": "━━━ データ抽出の例 ━━━", "info": "", "x": 160, "y": 320, "wires": [] }, { "id": "xml_inject3", "type": "inject", "z": "xml_sample_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 380, "wires": [["xml_template2"]] }, { "id": "xml_template2", "type": "template", "z": "xml_sample_tab", "name": "センサーXML", "field": "payload", "fieldType": "msg", "format": "text", "syntax": "plain", "template": "<sensors>\n <sensor id=\"temp01\" type=\"temperature\">\n <value unit=\"celsius\">25.5</value>\n <timestamp>2024-01-15T10:30:00</timestamp>\n </sensor>\n <sensor id=\"hum01\" type=\"humidity\">\n <value unit=\"percent\">60</value>\n <timestamp>2024-01-15T10:30:00</timestamp>\n </sensor>\n</sensors>", "output": "str", "x": 290, "y": 380, "wires": [["xml_parse2"]] }, { "id": "xml_parse2", "type": "xml", "z": "xml_sample_tab", "name": "XML→Obj", "property": "payload", "attr": "", "chr": "", "x": 450, "y": 380, "wires": [["xml_func2"]] }, { "id": "xml_func2", "type": "function", "z": "xml_sample_tab", "name": "温度抽出", "func": "// センサーデータから温度を抽出\nconst sensors = msg.payload.sensors.sensor;\nconst tempSensor = sensors.find(s => s.$.type === \"temperature\");\n\nmsg.payload = {\n sensorId: tempSensor.$.id,\n value: parseFloat(tempSensor.value[0]._),\n unit: tempSensor.value[0].$.unit,\n timestamp: tempSensor.timestamp[0]\n};\nreturn msg;", "outputs": 1, "timeout": 0, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 600, "y": 380, "wires": [["xml_debug3"]] }, { "id": "xml_debug3", "type": "debug", "z": "xml_sample_tab", "name": "温度データ", "active": true, "tosidebar": true, "console": false, "tostatus": true, "complete": "payload", "targetType": "msg", "statusVal": "payload.value", "statusType": "msg", "x": 770, "y": 380, "wires": [] } ]

フローの読み解き

応用フローは、3つの部分に分かれています。各部分の先頭にある comment ノードの名前が、下の表の「部分」に当たります。3つの inject はどれも「実行」という同じ名前なので、comment ノードのすぐ下の行にあるものを押してください。

部分 ノードの並び 見どころと、debug に出るもの パターン
XML → オブジェクト変換 「実行」(inject)→「XML文字列」(template)→「XML→Obj」(xml)→「オブジェクト出力」(debug) 本(book)が2冊入ったカタログの XML を変換する。同じ名前の要素が2つあるので、catalog.book は2要素の配列になる。属性のある <price currency="JPY">2500</price> は、{"_":"2500","$":{"currency":"JPY"}} のように、中の文字が _ に、属性が $ に分かれる パターン1
オブジェクト → XML変換 「実行」(inject)→「オブジェクト生成」(function)→「Obj→XML」(xml)→「XML出力」(debug) function が、注文のオブジェクトを msg.payload に入れる。"$": { "id": "ORD-001" } と書いた部分が属性になり、配列の item は同じ名前の要素の繰り返しになる。出力は <order id="ORD-001"><customer>田中商店</customer><items><item>…</item><item>…</item></items><total>5000</total></order>(先頭に XML 宣言が付く)。最上位のキーが order の1つだけなので、<root> は付かない パターン2
データ抽出の例 「実行」(inject)→「センサーXML」(template)→「XML→Obj」(xml)→「温度抽出」(function)→「温度データ」(debug) センサー2台分の XML をオブジェクトにしてから、function で type 属性が temperature のセンサーを探し、ID・値・単位・時刻を取り出す。出力は {"sensorId":"temp01","value":25.5,"unit":"celsius","timestamp":"2024-01-15T10:30:00"}。コードの value[0]._ や timestamp[0] に [0] が要るのは、子要素が配列になるため(公式サンプルの例2) パターン3

パターン1: XML → オブジェクト変換

応用フローの「XML → オブジェクト変換」を参照してください。

XML文字列 → XML→Obj → オブジェクト

用途: XML形式のデータをオブジェクトに変換

ポイント:

パターン2: オブジェクト → XML変換

応用フローの「オブジェクト → XML変換」を参照してください。

オブジェクト生成 → Obj→XML → XML文字列

用途: オブジェクトのデータをXML形式の文字列に変換

ポイント: 属性をつける場合は $ プロパティを使用

パターン3: データ抽出の例

応用フローの「データ抽出の例」を参照してください。

センサーXML → XML→Obj → 温度抽出 → 温度データ

用途: XMLに含まれる特定の項目だけを抽出

ポイント:

5. 演習問題

演習1: 基本的なXML変換 初級

課題: シンプルなXML文字列をオブジェクトに変換してください。

💼 使いどころ: XML形式のデータをオブジェクトとして扱い、含まれる値を取り出す場面で使います。

成功の条件:

💡 ヒント

Template ノードでXML文字列を作成し、XML ノードで変換します。

✅ 解答例フロー
[ {"id": "ex1_tab", "type": "tab", "label": "演習1"}, {"id": "ex1_inject", "type": "inject", "z": "ex1_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex1_template"]]}, {"id": "ex1_template", "type": "template", "z": "ex1_tab", "name": "XML", "field": "payload", "fieldType": "msg", "format": "text", "syntax": "plain", "template": "<user><name>山田太郎</name><email>yamada@example.com</email></user>", "output": "str", "x": 270, "y": 100, "wires": [["ex1_xml"]]}, {"id": "ex1_xml", "type": "xml", "z": "ex1_tab", "name": "XML→Obj", "property": "payload", "attr": "", "chr": "", "x": 410, "y": 100, "wires": [["ex1_debug"]]}, {"id": "ex1_debug", "type": "debug", "z": "ex1_tab", "name": "結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 550, "y": 100, "wires": []} ]

演習2: オブジェクトからXML生成 初級

課題: JavaScriptオブジェクトからXML文字列を生成してください。

💼 使いどころ: 商品情報などのオブジェクトをXML形式の文字列に変換する場面で使います。

成功の条件:

💡 ヒント

Change ノードで JSON 値型を使ってオブジェクトを設定し、XML ノードで変換します。

✅ 解答例フロー
[ {"id": "ex2_tab", "type": "tab", "label": "演習2"}, {"id": "ex2_inject", "type": "inject", "z": "ex2_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex2_change"]]}, {"id": "ex2_change", "type": "change", "z": "ex2_tab", "name": "オブジェクト", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\"product\":{\"name\":\"ノートPC\",\"price\":\"98000\",\"stock\":\"15\"}}", "tot": "json"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 280, "y": 100, "wires": [["ex2_xml"]]}, {"id": "ex2_xml", "type": "xml", "z": "ex2_tab", "name": "Obj→XML", "property": "payload", "attr": "", "chr": "", "x": 430, "y": 100, "wires": [["ex2_debug"]]}, {"id": "ex2_debug", "type": "debug", "z": "ex2_tab", "name": "XML出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 580, "y": 100, "wires": []} ]

演習3: 属性付きXMLの処理 中級

課題: 属性を持つXMLを解析し、属性値を取得してください。

💼 使いどころ: XMLタグの属性に入っている商品IDや通貨などの値を取り出す場面で使います。

成功の条件:

💡 ヒント

属性は $ プロパティに格納されます。Change ノードの JSONata 式を使って、ネストしたプロパティから値を抽出できます。

✅ 解答例フロー
[ {"id": "ex3_tab", "type": "tab", "label": "演習3"}, {"id": "ex3_inject", "type": "inject", "z": "ex3_tab", "name": "実行", "props": [], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex3_template"]]}, {"id": "ex3_template", "type": "template", "z": "ex3_tab", "name": "属性付きXML", "field": "payload", "fieldType": "msg", "format": "text", "syntax": "plain", "template": "<item id=\"ITM001\" category=\"electronics\">\n <name>スマートフォン</name>\n <price currency=\"JPY\">50000</price>\n</item>", "output": "str", "x": 290, "y": 100, "wires": [["ex3_xml"]]}, {"id": "ex3_xml", "type": "xml", "z": "ex3_tab", "name": "XML→Obj", "property": "payload", "attr": "", "chr": "", "x": 450, "y": 100, "wires": [["ex3_change"]]}, {"id": "ex3_change", "type": "change", "z": "ex3_tab", "name": "属性抽出", "rules": [{"t": "set", "p": "payload", "pt": "msg", "to": "{\n \"id\": payload.item.`$`.id,\n \"category\": payload.item.`$`.category,\n \"name\": payload.item.name[0],\n \"price\": payload.item.price[0].`_`,\n \"currency\": payload.item.price[0].`$`.currency\n}", "tot": "jsonata"}], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 600, "y": 100, "wires": [["ex3_debug"]]}, {"id": "ex3_debug", "type": "debug", "z": "ex3_tab", "name": "抽出結果", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 750, "y": 100, "wires": []} ]

演習4: 属性付きXMLの生成 上級

課題: 属性を持つXMLをオブジェクトから生成してください。

💼 使いどころ: ステータスコードやユーザーIDなど、属性を持つデータをXML形式で出力する場面で使います。

成功の条件:

💡 ヒント

属性を設定するには "$": { "attrName": "value" } 形式でオブジェクトを作成します。オブジェクトは、Inject ノードの msg.payload に「JSON」型で直接書けます。

✅ 解答例フロー
[ {"id": "ex4_tab", "type": "tab", "label": "演習4"}, {"id": "ex4_inject", "type": "inject", "z": "ex4_tab", "name": "実行(属性付きObj)", "props": [{"p": "payload"}], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "x": 130, "y": 100, "wires": [["ex4_xml"]], "payload": "{\"response\":{\"$\":{\"status\":\"success\",\"code\":\"200\"},\"data\":{\"user\":{\"$\":{\"id\":\"U001\"},\"name\":\"テストユーザー\"}}}}", "payloadType": "json"}, {"id": "ex4_xml", "type": "xml", "z": "ex4_tab", "name": "Obj→XML", "property": "payload", "attr": "", "chr": "", "x": 330, "y": 100, "wires": [["ex4_debug"]]}, {"id": "ex4_debug", "type": "debug", "z": "ex4_tab", "name": "XML出力", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "x": 490, "y": 100, "wires": []} ]

6. まとめ

重要ポイント:

注意事項:

7. 実務活用例

ケース1: SOAP API連携

SOAP Webサービスとの通信でXMLリクエスト/レスポンスを処理。

ケース2: 設定ファイル管理

XML形式の設定ファイルを読み込み、設定値をフローで使用。

ケース3: レガシーシステム連携

XML形式でデータ交換を行う古いシステムとの連携。

ケース4: RSS/Atomフィード処理

RSSフィードを解析して最新記事を取得・表示。

📚 Node-RED 公式ドキュメント

🏠