javascriptxmldomparserxmldom

Read XML attribute text and replace some words in the middle of text


I am trying to read XML node attribute value as string and replace some words in the middle of the retrieved text. However so far I was failed to make this success. Let me elaborate my issue.

I am reading external XML file (using fs) and reading content using dom parser. and I am trying to replace some words which contains in the one of node's attribute's text. Sample of what I am trying to do is as below.

<!DOCTYPE html>
<html>
<body>    
<p id="demo"></p>    
<script type="text/javascript">  

var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>test title</title>" +
"<author biography="Video provides a powerful way to help you prove your point.&#x28;&#x29; When you click Online Video, you can paste in the embed code for the video you want to add. You can also type a keyword to search online for the video that best fits your document.&#x28;&#x29; To make your document look professionally produced, Word provides header, footer, cover page, and text box designs that complement each other.&#x28;&#x29;For example, you can add a matching cover page, header, and sidebar.">Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");

var xy0 = xmlDoc.getElementsByTagName('author');
var xy1 = xy0.getAttribute('biography');
var xy2= xy1.replace("&#xd;&#xa;","<br><br>");
console.log(xy2);

</script>    
</body>
</html>

enter image description here

However this does not replace text as expected. Is it possible to achieve this. If not what are the possible alternatives? Please be kind enough to shed some light in this. Thanks in advance


Solution

  • Finally I was able to identify the issue. JavaScript does not recognize "&#xd;&#xa;" it recognize it as new line. So fix is simple, I just have to replace "&#xd;&#xa;" with \n

    var xy2= xy1.replace("\n","<br>");