xxxxxxxxxx
// Converts XML to JSON
// from: https://coursesweb.net/javascript/convert-xml-json-javascript_s2
function XMLtoJSON() {
var me = this; // stores the object instantce
// gets the content of an xml file and returns it in
me.fromFile = function(xml, rstr) {
// Cretes a instantce of XMLHttpRequest object
var xhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
// sets and sends the request for calling "xml"
xhttp.open("GET", xml ,false);
xhttp.send(null);
// gets the JSON string
var json_str = jsontoStr(setJsonObj(xhttp.responseXML));
// sets and returns the JSON object, if "rstr" undefined (not passed), else, returns JSON string
return (typeof(rstr) == 'undefined') ? JSON.parse(json_str) : json_str;
}
// returns XML DOM from string with xml content
me.fromStr = function(xml, rstr) {
// for non IE browsers
if(window.DOMParser) {
var getxml = new DOMParser();
var xmlDoc = getxml.parseFromString(xml,"text/xml");
}
else {
// for Internet Explorer
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
}
// gets the JSON string
var json_str = jsontoStr(setJsonObj(xmlDoc));
// sets and returns the JSON object, if "rstr" undefined (not passed), else, returns JSON string
return (typeof(rstr) == 'undefined') ? JSON.parse(json_str) : json_str;
}
// receives XML DOM object, returns converted JSON object
var setJsonObj = function(xml) {
var js_obj = {};
if (xml.nodeType == 1) {
if (xml.attributes.length > 0) {
js_obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
js_obj["@attributes"][attribute.nodeName] = attribute.value;
}
}
} else if (xml.nodeType == 3) {
js_obj = xml.nodeValue;
}
if (xml.hasChildNodes()) {
for (var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof(js_obj[nodeName]) == "undefined") {
js_obj[nodeName] = setJsonObj(item);
} else {
if (typeof(js_obj[nodeName].push) == "undefined") {
var old = js_obj[nodeName];
js_obj[nodeName] = [];
js_obj[nodeName].push(old);
}
js_obj[nodeName].push(setJsonObj(item));
}
}
}
return js_obj;
}
// converts JSON object to string (human readablle).
// Removes '\t\r\n', rows with multiples '""', multiple empty rows, ' "",', and " ",; replace empty [] with ""
var jsontoStr = function(js_obj) {
var rejsn = JSON.stringify(js_obj, undefined, 2).replace(/(\\t|\\r|\\n)/g, '').replace(/"",[\n\t\r\s]+""[,]*/g, '').replace(/(\n[\t\s\r]*\n)/g, '').replace(/[\s\t]{2,}""[,]{0,1}/g, '').replace(/"[\s\t]{1,}"[,]{0,1}/g, '').replace(/\[[\t\s]*\]/g, '""');
return (rejsn.indexOf('"parsererror": {') == -1) ? rejsn : 'Invalid XML format';
}
};
// creates object instantce of XMLtoJSON
var xml2json = new XMLtoJSON();
xxxxxxxxxx
// Converts XML to JSON
// from: https://coursesweb.net/javascript/convert-xml-json-javascript_s2
function XMLtoJSON() {
var me = this; // stores the object instantce
// gets the content of an xml file and returns it in
me.fromFile = function(xml, rstr) {
// Cretes a instantce of XMLHttpRequest object
var xhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
// sets and sends the request for calling "xml"
xhttp.open("GET", xml ,false);
xhttp.send(null);
// gets the JSON string
var json_str = jsontoStr(setJsonObj(xhttp.responseXML));
// sets and returns the JSON object, if "rstr" undefined (not passed), else, returns JSON string
return (typeof(rstr) == 'undefined') ? JSON.parse(json_str) : json_str;
}
// returns XML DOM from string with xml content
me.fromStr = function(xml, rstr) {
// for non IE browsers
if(window.DOMParser) {
var getxml = new DOMParser();
var xmlDoc = getxml.parseFromString(xml,"text/xml");
}
else {
// for Internet Explorer
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
}
// gets the JSON string
var json_str = jsontoStr(setJsonObj(xmlDoc));
// sets and returns the JSON object, if "rstr" undefined (not passed), else, returns JSON string
return (typeof(rstr) == 'undefined') ? JSON.parse(json_str) : json_str;
}
// receives XML DOM object, returns converted JSON object
var setJsonObj = function(xml) {
var js_obj = {};
if (xml.nodeType == 1) {
if (xml.attributes.length > 0) {
js_obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
js_obj["@attributes"][attribute.nodeName] = attribute.value;
}
}
} else if (xml.nodeType == 3) {
js_obj = xml.nodeValue;
}
if (xml.hasChildNodes()) {
for (var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof(js_obj[nodeName]) == "undefined") {
js_obj[nodeName] = setJsonObj(item);
} else {
if (typeof(js_obj[nodeName].push) == "undefined") {
var old = js_obj[nodeName];
js_obj[nodeName] = [];
js_obj[nodeName].push(old);
}
js_obj[nodeName].push(setJsonObj(item));
}
}
}
return js_obj;
}
// converts JSON object to string (human readablle).
// Removes '\t\r\n', rows with multiples '""', multiple empty rows, ' "",', and " ",; replace empty [] with ""
var jsontoStr = function(js_obj) {
var rejsn = JSON.stringify(js_obj, undefined, 2).replace(/(\\t|\\r|\\n)/g, '').replace(/"",[\n\t\r\s]+""[,]*/g, '').replace(/(\n[\t\s\r]*\n)/g, '').replace(/[\s\t]{2,}""[,]{0,1}/g, '').replace(/"[\s\t]{1,}"[,]{0,1}/g, '').replace(/\[[\t\s]*\]/g, '""');
return (rejsn.indexOf('"parsererror": {') == -1) ? rejsn : 'Invalid XML format';
}
};
// creates object instantce of XMLtoJSON
var xml2json = new XMLtoJSON();
xxxxxxxxxx
<soap:Body>
<platformMsgs:search
xmlns:platformMsgs="urn:messages_2022_1.platform.webservices.netsuite.com"
xmlns="urn:messages_2022_1.platform.webservices.netsuite.com">
<platformMsgs:searchRecord xsi:type="ns1:AccountingPeriodSearch"
xmlns:ns1="urn:accounting_2022_1.lists.webservices.netsuite.com">
<ns1:basic xsi:type="ns2:AccountingPeriodSearchBasic"
xmlns:ns2="urn:common_2022_1.platform.webservices.netsuite.com">
<ns2:isYear xsi:type="platformCore:SearchBooleanField"
xmlns:platformCore="urn:core_2022_1.platform.webservices.netsuite.com">
<platformCore:searchValue xsi:type="xsd:boolean">
<platformCore:value>false</platformCore:value>
</platformCore:searchValue>
</ns2:isYear>
<ns2:isAdjust xsi:type="platformCore:SearchBooleanField"
xmlns:platformCore="urn:core_2022_1.platform.webservices.netsuite.com">
<platformCore:searchValue xsi:type="xsd:boolean">
<platformCore:value>false</platformCore:value>
</platformCore:searchValue>
</ns2:isAdjust>
</ns1:basic>
</platformMsgs:searchRecord>
</platformMsgs:search>
</soap:Body>
xxxxxxxxxx
import json
import xml.etree.ElementTree as ET
def xml_to_json(xml_string):
root = ET.fromstring(xml_string)
def parse_element(element):
data = {}
if element.text:
data[element.tag] = element.text
else:
data[element.tag] = {}
for child in element:
child_data = parse_element(child)
if child.tag in data[element.tag]:
if isinstance(data[element.tag][child.tag], list):
data[element.tag][child.tag].append(child_data[child.tag])
else:
data[element.tag][child.tag] = [data[element.tag][child.tag], child_data[child.tag]]
else:
data[element.tag].update(child_data)
return data
return json.dumps(parse_element(root))
# Sample XML string
xml_string = '''
<root>
<person>
<name>John</name>
<age>30</age>
<city>New York</city>
<pets>
<dog>
<name>Buddy</name>
<age>5</age>
</dog>
<dog>
<name>Rex</name>
<age>3</age>
</dog>
</pets>
</person>
</root>
'''
json_output = xml_to_json(xml_string)
print(json_output)