472,354 Members | 1,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

how to read data from XMLDocument by using Javascript


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
  3. var xmlDoc=xmlHttp.responseXML.documentElement;
  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'
  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
5 24269
16,027 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. }
  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
213 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 ) { 
  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. } ;
  21. var xmlString = "";
  22. var xmltag = "";
  23. var currentChildNode = window.document;
  25. xmlString = loadXMLFromDOM2XMLString( xmlString, xmltag, currentChildNode );
  26. xmlString;
Dec 24 '10 #6

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

Similar topics

by: Tim | last post by:
I've been working on this for over a week now, and just can't get this figured out. Hoping one of you gurus can help me out here. I have an xml data island that I'm representing on an htm page as...
by: Graham Pengelly | last post by:
Hi I am trying to transform on System.Xml.XmlDocument into another using XslTransform without writing the object out to a file. I am guessing it should work something like this... public...
by: SQLScott | last post by:
I have a Web Service in which I am trying to pass an XMLDocument as a parameter to one of the methods. I would like to use the XMLTextReader to read the XML but I am getting the following error: ...
by: Chua Wen Ching | last post by:
Hi there, I have some problems when reading XML file. 1. First this, is what i did, cause i can't seem to read "sub elements or tags" values, so i place those values into attributes like this....
by: David Dvali | last post by:
Hello. I have such XML file: <root> <elem1> <optional> Some data <optional> </elem1> </root>
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
by: Gerrit | last post by:
It must be simple, but I don't find how I can read a XmlFile in an ArrayList. Sample of my XmlFile: <?xml version="1.0" encoding="utf-8" ?> <Relations> <Person> <FirstName>John</FirstName>...
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, can someone please let me know how I can read xml elements using object oriented program. I created a class to use the get and set properties however I dont know how I can pass the values from...
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.