gpt4 book ai didi

javascript - DOMParser 无法解析某些节点?

转载 作者:行者123 更新时间:2023-11-28 16:28:29 25 4
gpt4 key购买 nike

我正在为 Google Chrome 创建一个插件。我尝试解析以下 xml:

<?xml version="1.0" encoding="utf-8"?>
<anime>
<entry>
<id>9938</id>
<title>Ikoku Meiro no Crois&Atilde;&copy;e</title>
<english>Crois&Atilde;&copy;e in a Foreign Labyrinth ~ The Animation</english>
<synonyms>Ikoku Meiro no Crois&Atilde;&copy;e The Animation; Ikoku Meiro No Croisee The Animation; La crois&Atilde;&copy;e dans un labyrinthe &Atilde;&copy;tranger Special</synonyms>
<episodes>12</episodes>
<score>7.72</score>
<type>TV</type>
<status>Currently Airing</status>
<start_date>2011-07-04</start_date>
<end_date>0000-00-00</end_date>
<synopsis>The story takes place in the second half of the 19th century, as Japanese culture gains popularity in the West. A young Japanese girl, Yune, accompanies a French traveller, Oscar, on his journey back to France, and offers to help at the family&amp;#039;s ironwork shop in Paris. Oscar&amp;#039;s nephew and shop-owner Claude reluctantly accepts to take care of Yune, and we learn how those two, who have so little in common, get to understand each other and live together in the Paris of the 1800s.</synopsis>
<image>http://cdn.myanimelist.net/images/anime/8/29031.jpg</image>
</entry>
</anime>

使用此代码:

var parser = new DOMParser();
var xmlText = response.value;
var doc = parser.parseFromString(xmlText, "text/xml");
var entries = doc.getElementsByTagName("entry");

for (var i = 0; i < entries.length; ++i) {
var node = entries[i];

var titles = node.getElementsByTagName("title");
console.log("titles.length: " + titles.length);
if (titles.length > 0) {
console.log("title: " + titles[0].childNodes[0].nodeValue);
}

var scores = node.getElementsByTagName("score");
console.log("scores.length: " + scores.length);
if (scores.length > 0) {
console.log("score: " + scores[0].childNodes[0].nodeValue);
}

var ids = node.getElementsByTagName("id");
console.log("ids.length: " + ids.length);
if (ids.length > 0) {
console.log("id: " + ids[0].childNodes[0].nodeValue);
}
}

查看输出,似乎找到了 title 节点,但没有找到其内部文本。根本找不到 score 节点:

titles.length: 1
title:
scores.length: 0
ids.length: 1
id: 9938

有谁知道为什么会发生这种情况和/或如何解决它?

解决方法

我目前正在使用基于此解决方案的解决方法 answer :

function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

function xmlDecode(input){
var result = input;
result = result.replace(/</g, "&lt;");
result = result.replace(/>/g, "&gt;");
result = result.replace(/\n/g, "&#10;");
return htmlDecode(result);
}

// Usage:
var parser = new DOMParser();
var doc = parser.parseFromString(xmlDecode(xmlText), "text/xml");

我不确定这是否是最好的方法,但至少它让我走得更远。

最佳答案

我不确定这是否是问题的原因,但 XML 文档仅定义了五个命名实体:&< >"'’。将其他实体替换为它们要表示的字符(您的文档采用 UTF-8,使用 © 或其他此类字符是完全安全的)或数字实体(例如 &# 169;)。

或者,如果难以在文档中替换实体,您也可以定义自己的实体:

<!DOCTYPE anime [
<!ENTITY copy "&#169;">
]>

关于javascript - DOMParser 无法解析某些节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com