<script>
function xmlToJson(xml) {
let obj = {};
if (xml.nodeType === 1) { // Element
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (let j = 0; j < xml.attributes.length; j++) {
let attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType === 3) { // Text
obj = xml.nodeValue.trim();
}
if (xml.hasChildNodes()) {
for (let i = 0; i < xml.childNodes.length; i++) {
let item = xml.childNodes.item(i);
let nodeName = item.nodeName;
if (typeof obj[nodeName] === "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (!Array.isArray(obj[nodeName])) {
obj[nodeName] = [obj[nodeName]];
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
}
// Define XML string
let xmlString = `<source>
<publisher>Oorwin ATS</publisher>
<publisherurl>https://www.oorwin.com</publisherurl>
<lastbuilddate>2025-01-31 14:44:27</lastbuilddate>
<jobs/>
</source>`;
// Parse XML
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xmlString, "text/xml");
// Convert XML to JSON
let jsonResult = xmlToJson(xmlDoc.documentElement);
// Display JSON on the page
document.write("<pre>" + JSON.stringify(jsonResult, null, 4) + "</pre>");
</script>