0

I have some XML data that I need to handle in Node.js. I need to strip an envelope (SOAP) and then just return the payload/body och the envelope.

I get it as a DOM Document into my code and I can easily find the Body content and get that into a NodeList object.

Now I'd like to return the NodeList as a "XML String" but I haven't been able to find any way of doing this... What I basically need is a XML.stringify() (same as JSON.stringify()) but there doesn't seem to be any such function.

I've tried to write a "stringify()" myself but as there are attributes and namespaces in the XML it becomes very tricky...

1 Answer 1

1

This might help you out.

function nodeListToString(nodeList){
    return [].slice.call(nodeList).reduce((str, x) =>{
        return str+=x.outerHTML;
    }, '');
}

calling Array.prototype.slice.call() on a NodeList object converts it to an array, we can then use the arrays reduce method. reduce then iterates over everything in the array transforming it to the desired formt, read up on it here.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, you can also do [].slice.call() for shorter code.
Thanks! I am actually not using a "HTML" environment but a plain Node.js implementation so I don't have ".outerHTML" available it seems.
ah sorry, I haven't done much development is node, and am not sure what soap is. When you say you have a NodeList, is it an array of json objects like whats shown here npmjs.com/package/soap#overriding-the-attributes-key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.