467,077 Members | 1,137 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

how to read data from XMLDocument by using Javascript

hi

how to read data from XMLDocument by using Javascript. i got coding from internet. but its not read data. help me

its in JavaScript
-----------------------

Expand|Select|Wrap|Line Numbers
  1. //server return XML Document
  2.  
  3. var xmlDoc=xmlHttp.responseXML.documentElement;
  4.  
  5. document.getElementById("companyname").innerHTML= 
  6. xmlDoc.getElementsByTagName("compname")[0].childNodes[0].nodeValue; 
  7.     --- this line is used for read data from xmldocument. its correct or not'
  8.  
  9. document.getElementById("contactname").innerHTML=
  10. xmlDoc.getElementsByTagName("contname")[0].childNodes[0].nodeValue;
  11. document.getElementById("address").innerHTML=
  12. xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;
  13. document.getElementById("city").innerHTML=
  14. xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
  15. document.getElementById("country").innerHTML=
  16. xmlDoc.getElementsByTagName("country")[0].childNodes[0].nodeValue;
May 26 '07 #1
  • viewed: 23960
Share:
5 Replies
acoder
Expert Mod 8TB
There may be problems with white-space nodes - see this link.
Jan 24 '08 #2
Here is something which might help you: JavaScript Xml Document and XPath
Dec 29 '09 #3
Expand|Select|Wrap|Line Numbers
  1. function loadXMLString(xmlstring) {
  2.     if (window.DOMParser) {
  3.         parser=new DOMParser();
  4.         xmlDoc=parser.parseFromString(txt,"text/xml");
  5.     }
  6.     else // Internet Explorer
  7.     {
  8.         xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  9.         xmlDoc.async="false";
  10.         xmlDoc.loadXML(txt); 
  11.     }
  12.     return xmlDoc;
  13. }
  14.  
  15. var xmlDoc = loadXMLString("<a><b>1</b><b>2</b></a>") ;
  16. var objNodeList = xmlDoc.getElementsByTagName("a");
  17. alert(objNodeList.length);
Jan 1 '10 #4
xNephilimx
Expert 100+
You could also use jQuery, just wrap the xml code in the jquery object and you're ready to query it.

Best regards
Jan 4 '10 #5
Javascript for Selenium reading XML from DOM into XML string:
(when displaying xml file in a browser)

Expand|Select|Wrap|Line Numbers
  1. function loadXMLFromDOM2XMLString( xmlString, xmltag, currentChildNode ) { 
  2.  
  3. var nodes = currentChildNode.childNodes; 
  4. var i = 0 ;
  5. var node = nodes[i];
  6. var slash = '/';
  7. var lessthan = '<';
  8. var greaterthan = '>';
  9. while ( i < nodes.length) {
  10.    if (node.data == null) {xmltag = lessthan+node.localName+greaterthan;} else {xmltag = node.data;};
  11.    xmlString = xmlString  + xmltag;
  12.    xmlString = loadXMLFromDOM2XMLString( xmlString, xmltag, node  );   
  13.    if (node.data == null) {xmltag = lessthan+slash+node.localName+greaterthan;} else {xmltag = "";};
  14.    xmlString = xmlString + xmltag;
  15.    i++;
  16.    node = nodes[i];
  17. }
  18.  return xmlString ;
  19. } ;
  20.  
  21. var xmlString = "";
  22. var xmltag = "";
  23. var currentChildNode = window.document;
  24.  
  25. xmlString = loadXMLFromDOM2XMLString( xmlString, xmltag, currentChildNode );
  26. xmlString;
Dec 24 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Graham Pengelly | last post: by
1 post views Thread by Chua Wen Ching | last post: by
4 posts views Thread by David Dvali | last post: by
1 post views Thread by vkrasner | last post: by
4 posts views Thread by Gerrit | last post: by
6 posts views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post: by
2 posts views Thread by =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.