Javascript DOMParser and XMLSerialier removes XML entities -
I am trying to preserve some XML entities while parsing XML files in Javascript. The following code snippet shows the problem. Is there a way to create round trip paras and maintain XML entities (HTML)? This happens in Chrome FF and IE10
at aaa = '. & Lt; Root & gt; & Lt; Div & gt; & Amp; # 160; A & amp; # 160; Two & lt; / Div & gt; & Lt; / Root & gt; 'Dock' = New DOMParser () parseFromString (AAA 'application / xml') New XmlSerializer () serializeToString (doc). "& Lt; root> gt; div & gt; one to two & lt; / div & gt; & lt; / root & gt;" The problem is that I'm taking some parts out of HTML and storing them in XML, and then when I am doing, I want to get back the space in the XML. Edit it:. on str1 = new XmlSerializer () serializeToString: As donations and other people have pointed out, parser ascii code is 160, which looks like a simple space but for my eyes It changes (doc) str1.charCodeAt (15) 160
So where ever your application is losing the space, is it here?
You can use a hierarchical RegExp to render XML back to special characters, a good again As the usable function from:
function escapeExtended (s) {return s.replace (/ ([\ x80- \ xff]) / g, function (A, B) { Var C = b.charCodeAt (); Return "& #" + b.charCodeAt () + ";"}}); } At aaa = '& lt; Root & gt; & Lt; Div & gt; & Amp; # 160; A & amp; # 160; Two & lt; / Div & gt; & Lt; / Root> Var doc = new DOMParser (). ParseFromString (aaa, 'application / xml') var str = new XMLSerializer (). SerializeToString (doc); Alert (escapeExtended (STR)); // show: "Root", , & amp; # 160; one & amp; # 160; two & lt; / div & gt; & lt; / root & gt; Note that HTML entities (ex quot;) will lose the name of their symbol, and will convert to XML entities (& amp; # numumber; kind). You can not reverse the name without a large conversion table
Comments
Post a Comment