[英]getElementsByTagName stopping at triangle bracket
我在這種情況下使用getElementsByTagName
TheTitle = xmlDoc.getElementsByTagName("ArticleTitle")[i].childNodes[0].nodeValue;
獲取節點值但是當文本包含三角括號時,例如:
<ArticleTitle>"The Cat Sat on The <i>Mat</i>"</ArticleTitle>
我只能檢索
The Cat Sat on The
如何防止節點文本中的三角括號過早結束文本捕獲?
<ArticleTitle>"The Cat Sat on The <i>Mat</i>"</ArticleTitle>
有三個子節點
The Cat Sat on The
<i>
節點與<i>Mat</i>
"
所以, .childNodes[0].nodeValue;
當然,只是The Cat Sat on The
要修復,請使用:
TheTitle = xmlDoc.getElementsByTagName("ArticleTitle")[i].textContent;
反而
let doc = `<xml><ArticleTitle>"The Cat Sat on The <i>Mat</i>"</ArticleTitle></xml>`; let xmlDoc = new DOMParser().parseFromString(doc, 'text/xml'); let TheTitle = xmlDoc.getElementsByTagName("ArticleTitle")[0].textContent; console.log(TheTitle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.