user99999
user99999

Reputation: 2024

Replacing tag content with html string

I have the following xml:

<foo><toReplace/></foo>

I want to replace <toReplace/> tag with the following string:

"<b>bar</b>"

How can I do that?

Right now I have the following code:

var xml = "<foo><toReplace/></foo>";
var parser = new dom.DOMParser().parseFromString(xml, "text/xml");
parser.getElementsByTagName("toReplacce")[0].textNode = "<b>bar</b>";
console.log(parser.toString()); // "<foo>&lt;b>bar&lt;/b>"

The problem is that is escapes HTML. How can I replace the content with the HTML string here?

Upvotes: 0

Views: 1108

Answers (1)

Tal Avissar
Tal Avissar

Reputation: 10314

you can always use the module from npm

var unescape = require('unescape');

console.log(unescape(parser.toString()))

When I tested your code there is a small typo: (toReplacce instead of toReplace)

var dom = require('xmldom');

var xml = "<foo><toReplace/></foo>";
var parser = new dom.DOMParser().parseFromString(xml, "text/xml");
var a = parser.getElementsByTagName("toReplace")[0];
//console.dir(a);
a.textvalue = "<b>bar</b>";
console.log(parser.toString()); 

enter image description here

Upvotes: 1

Related Questions