YouTip LogoYouTip

Xml Dom

* * * DOM (Document Object Model) defines standard methods for accessing and manipulating documents. * * * ## XML DOM XML DOM (XML Document Object Model) defines standard methods for accessing and manipulating XML documents. XML DOM views an XML document as a tree structure. All elements can be accessed through the DOM tree. Their content can be modified or deleted, and new elements can be created. Elements, their text, and their attributes are all considered nodes. In our (#), you can learn more about XML DOM. * * * ## HTML DOM HTML DOM defines standard methods for accessing and manipulating HTML documents. All HTML elements can be accessed through the HTML DOM. In our (#), you can learn more about HTML DOM. * * * ## Loading an XML File - Cross-Browser Example The following example parses an XML document ("[note.xml](#)") into an XML DOM object, then extracts some information using JavaScript: ## Example

W3Schools Internal Note

To:
From:
Message:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","note.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to").childNodes.nodeValue; document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from").childNodes.nodeValue; document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body").childNodes.nodeValue; [Try it Β»](#) * * * ## Important Note! To extract the text "Tove" from the element in the XML file ("note.xml") above, the syntax is: getElementsByTagName("to").childNodes.nodeValue Please note that even if the XML file contains only one element, you must still specify the array index . This is because the getElementsByTagName() method returns an array. * * * ## Loading an XML String - Cross-Browser Example The following example parses an XML string into an XML DOM object, then extracts some information using JavaScript: ## Example

W3Schools Internal Note

To:
From:
Message:
txt=""; txt=txt+"Tove"; txt=txt+"Jani"; txt=txt+"Reminder"; txt=txt+"Don't forget me this weekend!"; txt=txt+""; if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(txt); } document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to").childNodes.nodeValue; document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from").childNodes.nodeValue; document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body").childNodes.nodeValue; [Try it Β»](#)
← Xml To HtmlProp Body Bgcolor β†’