Skip to main content

Hi Team,

I am getting XML as a response from REST callout and I am displaying it on LWC HTML using quick action on a custom object.

 

I am using the below code to beautify/align the XML code(and it got formatted when I view it using console.log) but the it is not displaying the XML response in formatted way in LWC UI

 

Code I have used to format XML:

 

formatXml(xml){

    var out = "";

    var tab = "    ";

    var indent = 0;

    var inClosingTag=false;

    var dent=function(no){

        out += "\n";

        for(var i=0; i < no; i++)

            out+=tab;

    }

 

    for (var i=0; i < xml.length; i++) {

        var c = xml.charAt(i);

        if(c=='<'){

            // handle </

            if(xml.charAt(i+1) == '/'){

                inClosingTag = true;

                dent(--indent);

            }

            out+=c;

        }else if(c=='>'){

            out+=c;

            // handle />

            if(xml.charAt(i-1) == '/'){

                out+="\n";

                //dent(--indent)

            }else{

              if(!inClosingTag)

                dent(++indent);

              else{

                out+="\n";

                inClosingTag=false;

              }

            }

        }else{

          out+=c;

        }

    }

    return out;

}

 

Format I am getting in console panel of browser(using console.log)

(Attached screenshots)

 

Code that I have written in HTML

 

<template>

<lightning-card title="XML for this order">

    <div>

         <p>

            <lightning-formatted-rich-text value={xmldata}></lightning-formatted-rich-text>

        </p>

        </div>

</lightning-card>

</template>

 

How to get formatted XML in LWC UI also? like I am getting in console.log

@Lightning Champion

0/9000