javascript - Getting XML information -
i'm having intense problems. i've found many questions similar , worked...for while. decided time ask own question.
i uploading xml file using filereader api , reading in string , looking elements , attributes so:
reader.onload = function (e) { var library = new string(e.target.result); if (window.domparser) { parser = new domparser(); xmldoc = parser.parsefromstring(library, "text/xml"); $(xmldoc).find('book').each(function () { pages = []; $(this).find("page").each(function () { page = []; try { page[0] = this.getattributenode("number").nodevalue; page[1] = this.getattributenode("words").nodevalue; } catch (err) { console.log(err); } pages.push(page); }); }); } }
and keep on getting either typeerror or this:
[09:11:08.523] typeerror: $(...).getattributenode not function
i don't understand why you're mixing jquery ($(this).each(...)
) , low-level dom (.getattributenode()
). stick homogeneous jquery: http://jsfiddle.net/kzcbe
var xmlmarkup = "<example>\n" + " <book>\n" + " <page number='p1' words='1104' />\n" + " <page number='p2' words='1230' />\n" + " </book>\n" + " <book>\n" + " <page number='p1' words='123' />\n" + " <page number='p2' words='145' />\n" + " </book>\n" + "</example>"; var library = new string(xmlmarkup); if (window.domparser) { parser = new domparser(); xmldoc = parser.parsefromstring(library, "text/xml"); $(xmldoc).find('book').each(function () { $(this).find("page").each(function () { var numberattr = $(this).attr("number"); var wordsattr = $(this).attr("words"); console.log("page number: " + numberattr); console.log("word count: " + wordsattr); }); }); }
Comments
Post a Comment