5

I'm having a little trouble getting the htmlparser2 module (using node.js) to output an xml string. Basically I'm parsing it in like so:

var htmlparser=require('htmlparser2');
function(xmlString,cb){
    var handler=new htmlparser.DomHandler(cb);
    var parser = new htmlparser.Parser(handler);
    parser.write(xmlString);
    parser.done();
}

Then I get an object called "dom", which I do some work on. After that work is done, I want to export it back into an XML string. I know that htmlparser.DomUtils.getOuterHTML(dom) works for HTML objects, but doesn't work for XML (at least not by default). I get back <undefined></undefined> when I call htmlparser.DomUtils.getOuterHTML(dom) on an xml dom.

Thanks in advance for any help you're able to offer! -Dylan

1
  • Fixed. Basically you need to use a second option in getOuterHTML... htmlparser.DomUtils.getOuterHTML(dom,{xmlMode:true}) If that doesn't work, try calling on the inner elems in the array like: htmlparser.DomUtils.getOuterHTML(dom[0],{xmlMode:true}) You'll have to wrap a for loop around it to get the whole document, but it works for me! Commented Jul 23, 2014 at 16:49

1 Answer 1

4

Fixed. Basically you need to use a second option in getOuterHTML...

htmlparser.DomUtils.getOuterHTML(dom,{xmlMode:true})

If that doesn't work, try calling on the inner elems in the array like:

htmlparser.DomUtils.getOuterHTML(dom[0],{xmlMode:true}) 

You'll have to wrap a for loop around it to get the whole document, but it works for me!

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

1 Comment

Thanks for sharing! Domutils still has no documentation, anything helps..

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.