java - Appending XML element as a text node (escaped) -
i need take xml element , children, escape xml , append them text node in document. know approach stupid, api requires it.
could accomplish using 1 of many xml libraries java? tried using jaxp, code doesn't escape apostrophes , double quotes.
… string content = elementtostring(nodetoescape); text text = document.createtextnode(content); node n = document.getelementsbytagname("targetnode").item(0); n.appendchild(text); } string elementtostring(element element) { document document = element.getownerdocument(); domimplementationls domimplls = (domimplementationls) document.getimplementation(); lsserializer serializer = domimplls.createlsserializer(); return serializer.writetostring(element); }
resulting document fragment:
<dataitem aggregate="none" name="order number"…
you don't need escape quotes , apostrophes. according to: http://www.w3schools.com/xml/xml_syntax.asp characters "<" , "&" strictly illegal in xml
Comments
Post a Comment