{"id":360,"date":"2009-05-27T21:46:35","date_gmt":"2009-05-28T02:46:35","guid":{"rendered":"http:\/\/eligrey.com\/?p=360"},"modified":"2009-05-27T21:46:35","modified_gmt":"2009-05-28T02:46:35","slug":"array-methods-for-xml","status":"publish","type":"post","link":"https:\/\/eligrey.com\/blog\/array-methods-for-xml\/","title":{"rendered":"Array methods for XML lists"},"content":{"rendered":"<p>I have created an open source library that implements every array method for E4X XML lists in JavaScript named <strong>e4x-array-methods.js<\/strong>. The methods output XML as opposed to arrays to make the output directly usable with other XML. To get the array representation of an XML list, use <code>slice<\/code>. This returns XML when used to slice out ranges from XML but if no arguments are passed or a third argument is specified which is equivalent to <code>true<\/code>, it will convert an XML list to an array instead. For example, <code>xmllist.slice(3, 5)<\/code> returns an XML list and <code>xmllist.slice(3, 5, true)<\/code> returns an array.<\/p>\n<h2 id=\"download\">Download<\/h2>\n<p>You can download e4x-array-methods.js at it&#8217;s <a href=\"http:\/\/github.com\/eligrey\/e4x-array-methods.js\/tree\/master\">github repository<\/a>. If you wish to minify the library yourself, make sure that your minification tool supports the E4X used in this library. Rhino-based minifiers fail, throwing syntax errors, and <a href=\"http:\/\/dean.edwards.name\/packer\/\">\/packer\/<\/a> errantly minifies all of the public methods names preceded by <code>.function::<\/code>.<\/p>\n<h2 id=\"examples\">Example<\/h2>\n<p>The following example demonstrate the usefulness of having array methods for XML lists. If you want to try out the example in a JavaScript shell, make sure <code>XML.prettyPrinting<\/code> is set to <code>false<\/code>, which removes any unnecessary whitespace from <code>xml.toString()<\/code> and <code>xml.toXMLString()<\/code>.<\/p>\n<pre lang=\"javascript\" escaped=\"true\">XML.prettyPrinting = false; \/\/ for simplifying the comparisons below\n\nvar foo = &lt;&gt;&lt;\/&gt;; \/\/ XMLList literal\nfoo.push(&lt;n&gt;0&lt;\/n&gt;, 1, 5, 3, 2, 6, 4);\nfoo.toXMLString() === \"&lt;n&gt;0&lt;\/n&gt;&lt;n&gt;1&lt;\/n&gt;&lt;n&gt;5&lt;\/n&gt;&lt;n&gt;3&lt;\/n&gt;&lt;n&gt;2&lt;\/n&gt;&lt;n&gt;6&lt;\/n&gt;&lt;n&gt;4&lt;\/n&gt;\";\nfoo.sort(function(a, b) {\n    return a - b;\n}).toXMLString() === \"&lt;n&gt;0&lt;\/n&gt;&lt;n&gt;1&lt;\/n&gt;&lt;n&gt;2&lt;\/n&gt;&lt;n&gt;3&lt;\/n&gt;&lt;n&gt;4&lt;\/n&gt;&lt;n&gt;5&lt;\/n&gt;&lt;n&gt;6&lt;\/n&gt;\";\nfoo.filter(function(x) { \/\/ filter out integers less then 3\n    if (!(x &lt; 3))\n        return true;\n}).toXMLString() === \"&lt;n&gt;3&lt;\/n&gt;&lt;n&gt;4&lt;\/n&gt;&lt;n&gt;5&lt;\/n&gt;&lt;n&gt;6&lt;\/n&gt;\";\nfoo.slice(2, 5).toXMLString() === \"&lt;n&gt;2&lt;\/n&gt;&lt;n&gt;3&lt;\/n&gt;&lt;n&gt;4&lt;\/n&gt;\";\nfoo.splice(2, 1, &lt;n&gt;9&lt;\/n&gt;, &lt;n&gt;8&lt;\/n&gt;).toXMLString() === \"&lt;n&gt;2&lt;\/n&gt;\";\nfoo.slice(2, 5).toXMLString() === \"&lt;n&gt;3&lt;\/n&gt;&lt;n&gt;9&lt;\/n&gt;&lt;n&gt;8&lt;\/n&gt;\";\nfoo.pop().toXMLString() === \"&lt;n&gt;6&lt;\/n&gt;\";\nfoo.shift().toXMLString() === \"&lt;n&gt;0&lt;\/n&gt;\";\nfoo.shift().toXMLString() === \"&lt;n&gt;1&lt;\/n&gt;\";\nfoo.unshift(&lt;bar\/&gt;);\nfoo.toXMLString() === \"&lt;bar\/&gt;&lt;n&gt;3&lt;\/n&gt;&lt;n&gt;9&lt;\/n&gt;&lt;n&gt;8&lt;\/n&gt;&lt;n&gt;4&lt;\/n&gt;&lt;n&gt;5&lt;\/n&gt;\";\nfoo.map(parseFloat).forEach(function(n){ print(n) }) \/\/ prints NaN, 3, 9, 8, 4, 5\nfoo.some(function(x) { \/\/ some are greater than 5\n    if (x &gt; 5) return true;\n}) === true;\nfoo.every(function(x) { \/\/ all are numbers\n    if (typeof x == \"number\") return true\n}) === false;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have created an open source library that implements every array method for E4X XML lists in JavaScript named e4x-array-methods.js. The methods output XML as opposed to arrays to make the output directly usable with other XML. To get the array representation of an XML list, use slice. This returns XML when used to slice [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[34,91],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-e4x","tag-javascript-libraries"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pfpUD-5O","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":0,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"wp:attachment":[{"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eligrey.com\/blog\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}