首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DOMParser无法解析某些节点?

DOMParser无法解析某些节点?
EN

Stack Overflow用户
提问于 2011-08-14 21:49:13
回答 1查看 2K关注 0票数 0

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

代码语言:javascript
复制
<?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>

使用以下代码:

代码语言:javascript
复制
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节点:

代码语言:javascript
复制
titles.length: 1
title: 
scores.length: 0
ids.length: 1
id: 9938

有人知道为什么会发生这种情况和/或如何修复它吗?

解决方法

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

代码语言:javascript
复制
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");

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

EN

回答 1

Stack Overflow用户

发布于 2011-08-14 21:55:58

我不确定这是否是您的问题的原因,但是XML文档只定义了5个命名实体:&amp;&lt;&gt;&quot;&apos;。将其他实体替换为它们应该表示的字符(您的文档采用UTF8格式,使用©或其他类似字符是完全安全的)或使用number实体(如&#169;)。

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

代码语言:javascript
复制
<!DOCTYPE anime [
    <!ENTITY copy "&#169;">
]>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7057187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档