0

I am successfully parsing an xml document in javascript, having read it in via an XMLHttpRequest I now want to grab one additional piece of info. The feed I'm reading contains this tag <opensearch:totalResults>118</opensearch:totalResults> How do I read the value it contains?

I've used Firefox's console to examine the xml doc's contents, looking in vain for where it's mentioned. Online searches don't help either.

Although not really relevant, the start of the heart of my code is:

    var process = function (xml) {
    var i, xmlDoc, table;
    xmlDoc = xml.responseXML;
    globXmlDoc = xmlDoc;
    var myItemAsAnObject = [];
    if (xmlDoc === null){
    feedLength = 0;
    } else {
    feedLength = xmlDoc.getElementsByTagName("item").length;
    if (feedLength == 0) { return false; }
    var x = xmlDoc.getElementsByTagName("item");

The XML is

<rss version="2.0">
<channel>
<title>victoriana: Zazzle.com Store: Matching "victoriana"</title>
<link>http://feed.zazzle.com/z.2/api/find.aspx?qs=victoriana&ft=rss&ou=%2Frss%3Fqs%3Dvictoriana</link>
<description/>
<language>en-us</language>
<pubDate>Thu, 04 Jul 2019 17:26:55 GMT</pubDate>
<ttl>60</ttl>
<opensearch:totalResults>118</opensearch:totalResults>
<opensearch:startIndex>1</opensearch:startIndex>
<opensearch:itemsPerPage>60</opensearch:itemsPerPage>
<opensearch:Query role="request" searchTerms="victoriana"/>
<item>
:
:
:

A link to the entire xml doc is: rss feed

Note that the globxmlDoc is a global var just to let me look at it in the console.

I don't really know where to start....

1 Answers1

1

Open this HTML doc and you will get 118 (Tested using Chrome)

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>Everyday 111</title>" +
"<author>Giada De Laurentiis</author>" +
"<opensearch:totalResults>118</opensearch:totalResults>" + 
"<year>2005</year>" +
"</book></bookstore>";

parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("opensearch:totalResults")[0].childNodes[0].nodeValue;
</script>

</body>
</html>
balderman
  • 22,927
  • 7
  • 34
  • 52
  • I already have the xmlDoc and am parsing it fine already. How would I use your approach on what I already have? See the heart of my code at the start. Thanks for your help on this, balderman :) – Mark Highton Ridley Jul 04 '19 at 20:13
  • I did try adding`feedTotalProdsAvail = xmlDoc.getElementsByTagName("opensearch:totalResults")[0].childNodes[0].nodeValue;` as the first statement in the else clause but it gave me the error TypeError: xmlDoc.getElementsByTagName(...)[0] is undefined – Mark Highton Ridley Jul 04 '19 at 20:23
  • I used your supplied code, balderman, and you're right, it does work in Chrome. But why not in Firefox? Obviously I'm after a method that works in all the major browsers. It does work also in Safari and Vivaldo browsers. – Mark Highton Ridley Jul 04 '19 at 21:23
  • Oh, I see now that Firefox complains that the `118` tag isn't bound to a namespace. – Mark Highton Ridley Jul 04 '19 at 21:56
  • I've ticked yours as the answer, balderman. In reality it's the best answer possible. The fault lies in the lack of a namespace definition in the rss feed I was trying to parse. So thank you for heloing me discover that :) – Mark Highton Ridley Jul 04 '19 at 22:19