ComfyUI Node

XML/JSON 互转

Convert XML to JSON and back without leaving ComfyUI

By spawner1145·Created about a year ago·Updated 11 months ago· 2
XML/JSON 互转
    • result_string
    text
    conversion_mode
    xml_root_nameelements

    The LLM era made this node's job more common than it sounds. Your vision model or chat API returns XML - a structure tag or a tool call, maybe a whole formatted report - and everything downstream in your ComfyUI workflow wants JSON. Or the reverse: you have JSON and something you're calling wants XML. XMLJSONConverter is the bridge: paste text in, pick a direction, get the converted string out.

    The controls:

    • text - the XML or JSON input (multiline).
    • conversion_mode - xml_to_json or json_to_xml. Both directions, one node.
    • xml_root_name (optional, default elements) - used in the json_to_xml direction: when your JSON isn't already a single-key dict, the converter wraps it under this root element name so the XML is well-formed.

    Under the hood it leans on the xmltodict library, which is the pragmatic way to do this and also the pack's reason for shipping that dependency. XML→JSON produces pretty-printed JSON with ensure_ascii=False, so CJK text stays readable. JSON→XML requires the JSON to be an object; anything else (a list, a scalar, or a dict with multiple top-level keys) gets wrapped in the root name first.

    The sharp edges are the ones you'd expect from XML in general, and they're worth knowing before you trust the output:

    • Attributes don't survive. xmltodict folds XML attributes into keys prefixed with @ (<img width="5"> becomes {"img": {"@width": "5"}}), and repeated elements can become lists or dicts depending on how many there are. The conversion is honest, but "converted XML" rarely looks the way a human would have written it.
    • Text mixed with child elements becomes a #text key. If you're converting XML with <p>hello <b>world</b></p> structure, the output has #text entries you'll need to handle downstream.
    • Errors don't throw - on a parse failure the node prints an error and returns the original input unchanged. That's forgiving, but it means "output equals input" is your signal that something failed, not an exception you'll see.

    It's a small, honest utility: use it when a string-based tool in your graph insists on one format and you have the other. It will not prettify your life beyond the conversion - it will not preserve everything, and the round-trip XML → JSON → XML is lossy. For one-off data-shape fixes between nodes, it's exactly right.

    Install

    ComfyUI Manager → search comfyui-spawner-nodes → install → restart. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/spawner1145/comfyui-spawner-nodes
    

    Deps: xmltodict (the engine) plus piexif and pypng. All light pure-Python, no models. The README is a stub ("some toolkits"); author spawner1145 is a Chinese-speaking dev known for a Wan2.1 SD extension. UI labels are Chinese; the mode values are the English strings xml_to_json / json_to_xml.

    Troubleshooting

    • Output is identical to input - the conversion failed (probably malformed XML/JSON) and the node returned the input untouched. Paste the text into a validator to find the syntax error.
    • json_to_xml wraps things in elements - you gave it a list or multi-key dict; it wrapped under xml_root_name to stay valid. Set that field to whatever root you actually want.
    • @attribute and #text keys appear in XML→JSON output - normal xmltodict behavior. Flatten them in json_process if a downstream node needs clean values.
    Categoryspawner/utils

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING
    conversion_modeCOMBO2 options: xml_to_json, json_to_xml
    xml_root_nameoptSTRINGelements

    Outputs (1)

    NameTypeDescription
    result_stringSTRING