/* libxmq - Copyright (C) 2026 Fredrik Öhrström (spdx: MIT) */

// Build a document with a string and an array, and convert it to json.

import { DOMImplementation } from "@xmldom/xmldom";
import { toJson } from "libxmq";

const doc = new DOMImplementation().createDocument(null, "request", null);
const request = doc.documentElement!;

// Without the attribute S the value 123 would become a json number.
const id = doc.createElement("id");
id.setAttribute("S", "");
id.appendChild(doc.createTextNode("123"));
request.appendChild(id);

// The attribute A makes an array, the elements named _ are its values.
const names = doc.createElement("names");
names.setAttribute("A", "");
for (const name of ["samuel", "isildur"])
{
    const n = doc.createElement("_");
    n.appendChild(doc.createTextNode(name));
    names.appendChild(n);
}
request.appendChild(names);

// toJson gives a javascript value, JSON.stringify the json text.
console.log(JSON.stringify(toJson(doc)));