开发者

<description> tag causes xml to stop parsing?

Can anyone see a problem with this code? My application works until I add these extra lines:

 var descriptionNode = item.getElementsByTagName("description")[0];
 var description = descriptionNode.firstChild.data;

var item = { ... 
             ...
             ...
             ...
             ...
             description: description,
             ...
           };

and to the view page

var descriptionNode = document.createElement("td");
                    descriptionNode.setAttribute("class", "description");
                    descriptionNode.appendChild(document.createTextNode(item["description"]));
                    tr.appendChild(descriptionNode);

...
<th scope="col">Description</th>

and to the rss.xml file

<description>This is a description</description>

Full snippet:

<html>
    <head>
        <script type="text/javascript">
            var items = []; // contains newest items
            var unsorted = []; // contains all items

            function init() {
                if (!localStorage.updateInterval) {
                    localStorage.updateInterval = 5;
                }

                if (!localStorage.types) {
                    localStorage.types = "audio:true;art:true;literature:true;";
                }

                chrome.browserAction.setBadgeBackgroundColor({color:[255, 102, 0, 255]});

                setTimeout(makeRequest, 3000);
            }

            function makeRequest() {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function() {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        parseItems(xmlhttp.responseXML);
                    }
                };

                xmlhttp.open("GET", "http://url.com/rss.xml", true);
                xmlhttp.send(null);

                setTimeout(makeRequest, 1000 * 60 * localStorage.updateInterval);
            }

            function parseItems(data) {
                var items = data.getElementsByTagName("item");

                for (var i = 0; i < items.length; i++) {
                    var item = items[i];

                    var dateNode = item.getElementsByTagName("pubDate")[0];
                    var date = formatDate(dateNode.firstChild.data);

                    var titleNode = item.getElementsByTagName("title")[0];
                    var title = formatTitle(titleNode.firstChild.data);

                    var type = titleNode.firstChild.data.split("[")[1].split("]")[0];

                    var linkNode = item.getElementsByTagName("link")[0];
                    var link = trimSpaces(linkNode.firstChild.data);

                    var authorNode = item.getElementsByTagName("author")[0];
                    var author = authorNode.firstChild.data;

                    var descriptionNode = item.getElementsByTagName("description")[0];
                    var description = descriptionNode.firstChild.data;

                    var item = { date: date, 
                                    title: title,
                                    featured: false,
                                    type: type, 
                                    author: author,
                                    description: description,
                                    link: link };

                    unsorted[i] = items;
                }

                processItems();
            }

...

view

    <script type="text/javascript">
        var items = [];
        var background;

        function init() {
            background = chrome.extension.getBackgroundPage();
            items = background.items;

            createItemTable();
        }

        function createitemTable() {
            var table = document.getElementById("item-table");
            var tbody = document.createElement("tbody");

            for (x in items) {
                var item = items[x];
                var tr = document.createElement("tr");

                if (item["featured"]) {
                    tr.setAttribute("class", "featured");
                    item["featured"] = false;
                }

                var dateNode = document.createElement("td");
                dateNode.setAttribute("class", "date");
                dateNode.appendChild(document.createTextNode(item["date"]));
                tr.appendChild(dateNode);

                var titleNode = document.createElement("td");
                titleNode.setAttribute("class", "title");
                titleNode.appendChild(document.createTextNode(item["title"]));
                tr.appendChild(titleNode);

                var typeNode = document.createElement("td");
                typeNode.setAttribute("class", "type");
                typeNode.appendChild(document.createTextNode(item["type"]));
                tr.appendChild(typeNode);

                var authorNode = document.createElement("td");
                authorNode.setAttribute("class", "author");
                authorNode.appendChild(document.createTextNode(item["author"]));
                tr.appendChild(authorNode);

                var descriptionNode = document.createElement("td");
                descriptionNode.setAttribute("class", "description");
                descriptionNode.appendChild(document.createTextNode(item["description"]));
                tr.appendChild(descriptionNode);

                tbody.appendChild(tr);
            }

            table.appendChild(tbody);
        }

        function openTab(url) {
            chrome.tabs.create({url: url});
        }
    </script>
</head>
<body onload="init();" onunload="background.updateBadge();">
    <img src="img/logo_popup.png" id="logo" alt="" title="" onclick="openTab('http://www.site.com/');" />
    <table id="item-table">
        <thead>
            <tr>
                <th scope="col">Date</th>
                <th scope="col">Title</th>
                <th scope="col">Type</th>
                <th scope="col">Author</th>
                <th scope="col">Description</th>
            </tr>
        </thead>
    </table>
</body>

Rss.xml

<rss version="2.0">
<channel>
  <title>Site TItle</title>
  <link>http://www.site.com</link>
  <language>en-us</language>
  <pubDate>Sun, 03 Jul 2011 14:40:50 +0000</pubDate>
  <lastBuildDate>Sun, 03 Jul 2011 14:40:50 +0000</lastBuildDate>
  <copyright/>

  <item>
    <title>
      [art] - This is a title
    </title>
    &l开发者_StackOverflowt;link>http://www.site.com/id?=333</link>
    <author>Author</author>
    <description>This is a description</description>
    <pubDate>Sun, 03 Jul 2011 12:38:12 +0000</pubDate>
  </item>

  <item>
    <title>
      [music] - This is a title
    </title>
    <link>http://www.site.com/id?=332</link>
    <author>Author</author>
    <description>This is a description</description>
    <pubDate>Sun, 03 Jul 2011 12:14:53 +0000</pubDate>
  </item>

</channel>
</rss>


Found the problem: When creating an xml file with php the database field description is empty on one of the entries which causes the tag to output like <description />. Fixing that to <description></description> solves the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜