例如如何使用nodejs動態地在xml文件中插入結束標記(如果不存在)?
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
在上面的XML </note>
錯過,如何查找和使用中的NodeJS XML動態添加錯過的結束標記。
例如如何使用nodejs動態地在xml文件中插入結束標記(如果不存在)?
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
在上面的XML </note>
錯過,如何查找和使用中的NodeJS XML動態添加錯過的結束標記。
您需要有一些解析器來解析輸入髒HTML並對其進行消毒。 您可以使用帶有髒HTML的字符串來提供DOMPurify,並且它將返回帶乾淨HTML的字符串。 檢查了這一點https://github.com/cure53/DOMPurify
var clean = DOMPurify.sanitize(dirtyHTML);
您還可以遊覽JSDOM和其他類似的DOM解析器庫。
爲了解析和驗證XML,需要schema definition (XSD)。
這樣,解析器能夠驗證的元素,並告訴你,如果有任何無效的 - 缺少,拼寫錯誤等
把你的例子 - 沒有XSD,你不會知道,如果note
可以包含任何額外的子元素,例如date
。
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<date>2016-01-01</date>
而用爲XSD,解析器會知道,一個note
元素將包含to
,from
,heading
和body
元素後,它會希望關閉note
標籤。
一旦您知道您的驗證問題在哪裏 - 例如缺少結束標記 - 您可以執行清理。
有對等的NodeJS儘可能多的XML解析器選項...
https://www.npmjs.com/package/libxml-xsd https://www.npmjs.com/package/jgexml
DOMPurify看起來更加的防範XSS?也看不到任何地方提到的XML。你之前用XML成功使用過它嗎? – timothyclifford
@timothyclifford你說得對。我提供的解決方案只適用於HTML。 – Rudra
可能仍然對OP有用,永遠不知道:) – timothyclifford