How to serialize browser HTML DOM to XML?
I need to serialize browser parsed HTML DOM to well-format XML.
In firefox (gecko), this works:
// serialize body to well-format XML.
var xml = new XMLSerializer().serializeToString(document.body);
But in webkit, result is equivalent to document.body.outerHTML, not well-format XML (for example: <br> won't开发者_开发问答 become <br />
)
How to serialize browser HTML DOM to XML in webkit?
Thanks.
I have a setInnerXHTML
method (not the Facebook version) which should work for this. The method is included in the base framework file, hemi.js
, available from the Hemi Project Page. It is also included in my older libXmlRequest
library.
Example:
var oXml = Hemi.xml.newXmlDocument("Xhtml");
Hemi.xml.setInnerXHTML(oXml.documentElement, document.documentElement, oXml);
var sSerial = Hemi.xml.serialize(oXml);
If you want to test this on a particular browser, navigate to the Hemi Project Page, click the upper-right tool icon, and click the Active Source tab. Copy and paste the sample code into the textarea and click Eval Source (the response will be a node name). Type in sSerial
into the input field and hit enter, or click Eval, and you should see the serialized XML of the copied HTML DOM.
精彩评论