• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Addon / getXML

getXML

getXML

Obtain the contents of an XML element and convert it to text. This script is great for using with Ajax-type applications, when it’s necessary to send back the contents of an element to the server without knowing what’s there.

/* This script and many more are available free online at
The JavaScript Source :: http://javascriptsource.com
Created by: Neal Venditto :: http://neal.venditto.org/ */

function getXML(aNode) {
  var out = ”;
  if(aNode.nodeType == Node.ELEMENT_NODE) {
    out = ‘<‘+aNode.nodeName;
    if(aNode.hasAttributes()) {
      var atts = aNode.attributes;
      for(var x=0;x<atts.length;x++) {
        out +=’ ‘+atts[x].nodeName+’=”‘+atts[x].nodeValue+'”‘;
      }
    }
    
    if(aNode.hasChildNodes()) {
      out += ‘>’;
      var kids = aNode.childNodes;
      for(var x=0;x<kids.length;x++) {
        switch(kids[x].nodeType) {
          case Node.ELEMENT_NODE:
            out += getXML(kids[x]);
            break;
          case Node.TEXT_NODE:
            out += kids[x].nodeValue;
            break;
          case Node.COMMENT_NODE:
            out += ‘<!–‘+kids[x].nodeValue+’–>’;
            break;
          case Node.CDATA_SECTION_NODE:
            out += ‘<‘+’![CDATA[‘+kids[x].nodeValue+’]’+’]>’;
            break;  
        }
      }
      out += ‘</’+aNode.nodeName+’>’;
    } else { 
      out += ‘/>’;
    }
  }  
  return out;
}

Addon

Related Snippets:

  • Multiple onLoad 2
  • Window Print Method
  • getElementsByAttribute
  • Remove HTML Tags

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers