簡體   English   中英

getElementsByTagName 在三角括號處停止

[英]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>有三個子節點

  1. 文本節點: The Cat Sat on The
  2. <i>節點與<i>Mat</i>
  3. 文本節點"

所以, .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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM