I have been handed a chunk of code from a developer that no longer works for us, and I am new to the regex game so to speak, so apologies if the answer seems simple.
the goal is to format an XML response into something that is readable and here is the code I have been presented with:
formatXml: function (xml) {
if (xml.indexOf('<') == -1) {
return xml;
}
//remove all white spaces
xml = xml.replace(/>s+|s+</g, function (m) {
return m.trim();
});
// remove extra variables
xml = xml.substring(xml.indexOf('<'), (xml.lastIndexOf('>') + 1));
var formatted = '';
var reg = /(>)(<)(/*)/g;
xml = xml.replace(reg, '$1rn$2$3');
var pad = 0;
jQuery.each(xml.split('rn'), function (index, node) {
var indent = 0;
if (node.match(/.+</w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^</w/)) {
if (pad != 0) {
pad -= 1;
}
} else if (node.match(/^<w[^>]*[^/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
var padding = '';
for (var i = 0; i < pad; i++) {
padding += ' ';
}
formatted += padding + node + 'rn';
pad += indent;
});
return formatted;
}
The xml
that is sent into this looks something like the following portion:
"\u003cns3:oadrPayload\n\t\t\t\t\txmlns\u003d\"http://www.w3.org/2000/09/xmldsig#\" \n\t\t\t\t\txmlns:ns2\u003d\"http://docs.oasis-open.org/ns/energyinterop/201110\" \n\t\t\t\t\txmlns:ns3\u003d\"http://openadr.org/oadr-2.0b/2012/07\" \n\t\t\t\t\txmlns:ns4\u003d\"http://docs.oasis-open.org/ns/energyinterop/201110/payloads\" \n\t\t\t\t\txmlns:ns5\u003d\"urn:ietf:params:xml:ns:icalendar-2.0\" \n\t\t\t\t\txmlns:ns6\u003d\"urn:ietf:params:xml:ns:icalendar-2.0:stream\" xmlns:ns7\u003d\"http://www.w3.org/2005/Atom\" \n\t\t\t\t\txmlns:ns8\u003d\"http://docs.oasis-open.org/ns/emix/2011/06/power\" \n\t\t\t\t\txmlns:ns9\u003d\"http://www.opengis.net/gml/3.2\" \n\t\t\t\t\txmlns:ns10\u003d\"http://docs.oasis-open.org/ns/emix/2011/06\" \n\t\t\t\t\txmlns:ns11\u003d\"http://docs.oasis-open.org/ns/emix/2011/06/siscale\" \n\t\t\t\t\txmlns:ns12\u003d\"http://www.w3.org/2009/xmldsig11#\" \n\t\t\t\t\txmlns:ns13\u003d\"http://openadr.org/oadr-2.0b/2012/07/xmldsig-properties\" \n\t\t\t\t\txmlns:ns14\u003d\"urn:un:unece:uncefact:codelist:standard:5:ISO42173A:2010-04-07\"\u003e\n\t\t\t\t\t\u003cns3:oadrSignedObject\u003e\n\t\t\t\t\t\t\u003cns3:oadrResponse ns2:schemaVersion\u003d\"2.0b\"\u003e\n\t\t\t\t\t\t\t\u003cns2:eiResponse\u003e\n\t\t\t\t\t\t\t\t\u003cns2:responseCode\u003e200\u003c/ns2:responseCode\u003e\n\t\t\t\t\t\t\t\t\u003cns2:responseDescription\u003eOK\u003c/ns2:responseDescription\u003e\n\t\t\t\t\t\t\t\t\u003cns4:requestID\u003e123456789abc\u003c/ns4:requestID\u003e\n\t\t\t\t\t\t\t\u003c/ns2:eiResponse\u003e\n\t\t\t\t\t\t\u003c/ns3:oadrResponse\u003e\n\t\t\t\t\t\u003c/ns3:oadrSignedObject\u003e\n\t\t\t\t\u003c/ns3:oadrPayload\u003e"
and what is being returned is this:
"u003cns3:oadrPayloadntttttxmlnsu003d"http://www.w3.org/2000/09/xmldsig#" ntttttxmlns:ns2u003d"http://docs.oasis-open.org/ns/energyinterop/201110" ntttttxmlns:ns3u003d"http://openadr.org/oadr-2.0b/2012/07" ntttttxmlns:ns4u003d"http://docs.oasis-open.org/ns/energyinterop/201110/payloads" ntttttxmlns:ns5u003d"urn:ietf:params:xml:ns:icalendar-2.0" ntttttxmlns:ns6u003d"urn:ietf:params:xml:ns:icalendar-2.0:stream" xmlns:ns7u003d"http://www.w3.org/2005/Atom" ntttttxmlns:ns8u003d"http://docs.oasis-open.org/ns/emix/2011/06/power" ntttttxmlns:ns9u003d"http://www.opengis.net/gml/3.2" ntttttxmlns:ns10u003d"http://docs.oasis-open.org/ns/emix/2011/06" ntttttxmlns:ns11u003d"http://docs.oasis-open.org/ns/emix/2011/06/siscale" ntttttxmlns:ns12u003d"http://www.w3.org/2009/xmldsig11#" ntttttxmlns:ns13u003d"http://openadr.org/oadr-2.0b/2012/07/xmldsig-properties" ntttttxmlns:ns14u003d"urn:un:unece:uncefact:codelist:standard:5:ISO42173A:2010-04-07"u003entttttu003cns3:oadrSignedObjectu003enttttttu003cns3:oadrResponse ns2:schemaVersionu003d"2.0b"u003entttttttu003cns2:eiResponseu003enttttttttu003cns2:responseCodeu003e200u003c/ns2:responseCodeu003enttttttttu003cns2:responseDescriptionu003eOKu003c/ns2:responseDescriptionu003enttttttttu003cns4:requestIDu003e123456789abcu003c/ns4:requestIDu003entttttttu003c/ns2:eiResponseu003enttttttu003c/ns3:oadrResponseu003entttttu003c/ns3:oadrSignedObjectu003enttttu003c/ns3:oadrPayloadu003e"
But it should be this:
"<ns3:oadrPayload
xmlns="http://www.w3.org/2000/09/xmldsig#"
xmlns:ns2="http://docs.oasis-open.org/ns/energyinterop/201110"
xmlns:ns3="http://openadr.org/oadr-2.0b/2012/07"
xmlns:ns4="http://docs.oasis-open.org/ns/energyinterop/201110/payloads"
xmlns:ns5="urn:ietf:params:xml:ns:icalendar-2.0"
xmlns:ns6="urn:ietf:params:xml:ns:icalendar-2.0:stream" xmlns:ns7="http://www.w3.org/2005/Atom"
xmlns:ns8="http://docs.oasis-open.org/ns/emix/2011/06/power"
xmlns:ns9="http://www.opengis.net/gml/3.2"
xmlns:ns10="http://docs.oasis-open.org/ns/emix/2011/06"
xmlns:ns11="http://docs.oasis-open.org/ns/emix/2011/06/siscale"
xmlns:ns12="http://www.w3.org/2009/xmldsig11#"
xmlns:ns13="http://openadr.org/oadr-2.0b/2012/07/xmldsig-properties"
xmlns:ns14="urn:un:unece:uncefact:codelist:standard:5:ISO42173A:2010-04-07">
<ns3:oadrSignedObject>
<ns3:oadrResponse ns2:schemaVersion="2.0b">
<ns2:eiResponse>
<ns2:responseCode>200</ns2:responseCode>
<ns2:responseDescription>OK</ns2:responseDescription>
<ns4:requestID>123456789abc</ns4:requestID>
</ns2:eiResponse>
</ns3:oadrResponse>
</ns3:oadrSignedObject>
</ns3:oadrPayload>"
Why is it that the regex in formatXml
doesn’t remove all of the escaped characters and the “” characters?
Also, what can I add in to this to be able to replace the u003d
with =
, u003c
with <
, and u003e
with >
?