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
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!