473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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
5 24369
acoder
16,027 Expert Mod 8TB
There may be problems with white-space nodes - see this link.
Jan 24 '08 #2
Hallow
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
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 ) { 
  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

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

Similar topics

5
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...
2
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...
7
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: ...
1
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....
4
by: David Dvali | last post by:
Hello. I have such XML file: <root> <elem1> <optional> Some data <optional> </elem1> </root>
1
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; ...
4
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>...
6
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.