XML DOM - Javatpoint
XML DOM - Javatpoint
1. <TABLE>
2. <ROWS>
3. <TR>
4. <TD>A</TD>
5. <TD>B</TD>
6. </TR>
7. <TR>
8. <TD>C</TD>
9. <TD>D</TD>
10. </TR>
11. </ROWS>
12. </TABLE>
note.xml
xmldom.html
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <h1>Important Note</h1>
5. <div>
6. <b>To:</b> <span id="to"></span>
<br>
7. <b>From:</b> <span id="from">
</span><br>
8. <b>Message:
</b> <span id="message"></span>
9. </div>
10. <script>
11. if (window.XMLHttpRequest)
12. {// code for IE7+, Firefox, Chrome, Ope
ra, Safari
13. xmlhttp=new XMLHttpRequest();
14. }
15. else
16. {// code for IE6, IE5
17. xmlhttp=new ActiveXObject("Microso
ft.XMLHTTP");
18. }
19. xmlhttp.open("GET","note.xml",false);
20. xmlhttp.send();
21. xmlDoc=xmlhttp.responseXML;
22. document.getElementById("to").inner
HTML=
23. xmlDoc.getElementsByTagName("to")
[0].childNodes[0].nodeValue;
24. document.getElementById("from").inn
erHTML=
25. xmlDoc.getElementsByTagName("from
")[0].childNodes[0].nodeValue;
26. document.getElementById("message")
.innerHTML=
27. xmlDoc.getElementsByTagName("bod
y")[0].childNodes[0].nodeValue;
28. </script>
29. </body>
30. </html>
Test it Now
Output:
Important Note
To: [email protected]
From: [email protected]
Message: Hello XML DOM
xmldom.html
1. <!DOCTYPE html>
2. <html>
3. <body>
4. <h1>Important Note2</h1>
5. <div>
6. <b>To:</b> <span id="to"></span>
<br>
7. <b>From:</b> <span id="from">
</span><br>
8. <b>Message:
</b> <span id="message"></span>
9. </div>
10. <script>
11. txt1="<note>";
12. txt2="<to>Sania Mirza</to>";
13. txt3="
<from>Serena William</from>";
14. txt4="
<body>Don't forget me this weekend!
</body>";
15. txt5="</note>";
16. txt=txt1+txt2+txt3+txt4+txt5;
17.
18. if (window.DOMParser)
19. {
20. parser=new DOMParser();
21. xmlDoc=parser.parseFromString(txt,"
text/xml");
22. }
23. else // Internet Explorer
24. {
25. xmlDoc=new ActiveXObject("Microsof
t.XMLDOM");
26. xmlDoc.async=false;
27. xmlDoc.loadXML(txt);
28. }
29. document.getElementById("to").inner
HTML=
30. xmlDoc.getElementsByTagName("to")
[0].childNodes[0].nodeValue;
31. document.getElementById("from").inn
erHTML=
32. xmlDoc.getElementsByTagName("from
")[0].childNodes[0].nodeValue;
33. document.getElementById("message")
.innerHTML=
34. xmlDoc.getElementsByTagName("bod
y")[0].childNodes[0].nodeValue;
35. </script>
36. </body>
37. </html>
Test it Now
Output:
Important Note2