API: xml
Source:
utils/xml.ts
Utility class for converting data between XML and JSON formats. It automatically handles input serialization if an object is passed instead of a string.
Usage
import { xml } from "sdk/utils";
import { response } from "sdk/http";
const jsonInput = {
firstName: "John",
lastName: "Doe",
bio: {
age: 24,
sex: "male"
}
};
const xmlInput =
"<person>" +
"<firstName>John</firstName>" +
"<lastName>Doe</lastName>" +
"<bio>" +
"<age>24</age>" +
"<sex>male</sex>" +
"</bio>" +
"</person>";
response.println(xml.fromJson(JSON.stringify(jsonInput)));
response.println(xml.toJson(xmlInput));
response.flush();
response.close();Classes
XML
Utility class for converting data between XML and JSON formats.
It automatically handles input serialization if an object is passed instead of a string.
Methods
fromJson
fromJson (input:string|any):stringConverts a JSON input (either a JSON string or a raw JavaScript object) into an XML string.
Note: If a JavaScript object is passed, it is first stringified using JSON.stringify().
@param input The JSON string or object to be converted to XML.
@returns The resulting XML content as a string.
toJson
toJson (input:string|any):stringConverts an XML input (expected as an XML string) into a JSON formatted string.
@param input The XML string to be converted to JSON.
@returns The resulting JSON content as a string.