locked
Getting information from an XML element with a namespace in Windows 8 JS app

    Question

  • If I have an XML file with the following:

    <item>
        <media:credit>...</media:credit>
        <media:text type="html">...</media:text>
        <title>...</title>
        <link>...</link>
        <description>...</description>
        <pubDate>...</pubDate>
        <category>...</category>
    </item>

    And the following code:

    WinJS.xhr({ url: "..." }).then(function (rss) {
        var items = rss.responseXML.querySelectorAll("item");
    
        for (var n = 0; n < items.length; n++) {
            var article = {};
            article.title = items[n].querySelector("title").textContent;
            var thumbs = items[n].querySelectorAll("media");
            article.content = items[n].querySelector("text").textContent;
            article.description = items[n].querySelector("description").textContent;
            article.subtitle = items[n].querySelector("pubDate").textContent;
            
            list.push({ group: BasketballGroups[0], title: article.title, subtitle: article.subtitle, description: article.description, content: article.content });
            }
        }
    });

    How would I reference the <media:text>...</media:text> element and extract its contents? I tried by just putting "text" in the querySelector, however, it kept returning as undefined (The error said that I was trying to get "textContent" of a null or undefined element).

    How would I get that element's info?

    Monday, March 24, 2014 4:36 PM

Answers

  • You should use an "escaped" format when specifying a parameter to the querySelector function. In the above case, you will need to use the following code:

    var credit = item.querySelector("media\\:credit").textContent;
    var text = item.querySelector("media\\:text").textContent;
    var textType = item.querySelector("media\\:text").attributes[0].textContent;


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, March 25, 2014 12:25 AM
    Moderator