skip to Main Content

I’m using this to use a XML document

_getXmlDocument: function(TextToParse) {
try {
            //XmlDoc = new ActiveXObject("Microsoft.XMLDOM")
            var XmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
            XmlDoc.async = "false"
            XmlDoc.loadXML(TextToParse);
        }
        catch(ex) {
            Efficy.log("_getXmlDocument - Cannot create an XML document: " + ex.description)
            //Firefox, Mozilla, Opera, etc.
            try {
                Parser = new DOMParser()
                XmlDoc = Parser.parseFromString(TextToParse,"text/xml")
            }
            catch(ex) {throw new Error("Cannot create an XML document: " + ex.description)}
        }
        return XmlDoc
    }

I would like to after getting a specific tag

privates._XmlGetElementsByTagName(data, "tagname")

to simply export the raw XML including the XML tags in a string.
But I cannot find a way to do this

I tried looping etc but no results

2

Answers


  1. Use XMLSerializer to change it back into a string.

    const note = `<?xml version="1.0" encoding="UTF-8"?>
    <note>
      <to>Tove</to>
      <from>Jani</from>
      <heading>Reminder</heading>
      <body>
        <para>Bacon ipsum dolor amet tail chuck beef, biltong capicola pork belly jowl tongue meatloaf brisket beef ribs picanha. Shankle meatloaf t-bone frankfurter, brisket salami short ribs ribeye chislic pancetta tongue meatball. Fatback swine ground round pork chop landjaeger tenderloin chislic tail. Short ribs tri-tip pig pork, doner meatball frankfurter biltong drumstick prosciutto tail alcatra capicola fatback porchetta. Short loin jowl venison rump pork loin buffalo, beef ribs sirloin drumstick. Pork loin pork chop cow jowl ham hock kielbasa landjaeger andouille bresaola meatloaf brisket. Venison tongue leberkas, turkey beef ribs pig turducken andouille salami jowl tri-tip beef cupim short ribs short loin.</para>
      <para>Beef ribs swine pork loin frankfurter, ham short loin turducken sausage brisket burgdoggen landjaeger strip steak tail. Jerky porchetta cupim venison rump spare ribs fatback ham brisket jowl flank. Leberkas fatback frankfurter sirloin ham pig tri-tip bresaola pastrami pork belly tenderloin meatloaf drumstick. Strip steak pork belly bacon, pig hamburger sausage picanha jerky drumstick sirloin tail pork loin landjaeger. Tenderloin strip steak frankfurter jerky. Cow ground round kevin filet mignon. Burgdoggen brisket turkey swine shoulder chicken strip steak chuck landjaeger.</para>
      </body>
    </note>
    `
    
    
    const parser = new DOMParser()
    const xmlDoc = parser.parseFromString(note,"text/xml");
    
    const body = xmlDoc.querySelector("body");
    
    const serializer = new XMLSerializer();
    const xmlStr = serializer.serializeToString(body);
    
    console.log(xmlStr);
    Login or Signup to reply.
  2. You can serialize a specific DOM node to XML.

    const xmlString = `<foo><bar atttribute="value">content</bar></foo>`;
    const xmlDocument = (new DOMParser).parseFromString(xmlString, 'text/xml');
    const serializer = new XMLSerializer();
    
    const output = Array.from(xmlDocument.getElementsByTagName('bar'))
      .map(
        (node) => serializer.serializeToString(node)
      )
      .join('');
      
    console.log(output);

    For node.js you can use @xmldom/xmldom.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search