473,387 Members | 1,453 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,387 software developers and data experts.

Recursion problem: while convert xml-document object to xml-string

here is a problem in recursion: unexpected result ?
by this program I just want to convert xml dom's document object to xml-string.
(for all browsers)

Expand|Select|Wrap|Line Numbers
  1.         //load a xml
  2.     function loadXMLDoc(dname)
  3.     {
  4.         var xmlDoc;
  5.         // code for IE
  6.         if (window.ActiveXObject)
  7.         {
  8.             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  9.         }
  10.         // code for Mozilla, Firefox, Opera, etc.
  11.         else if (document.implementation && document.implementation.createDocument)
  12.         {
  13.             xmlDoc=document.implementation.createDocument("","",null);
  14.         }
  15.         else
  16.         {
  17.             alert('Your browser cannot handle this script');
  18.         }
  19.         xmlDoc.async=false;
  20.         xmlDoc.load(dname);
  21.         return(xmlDoc);
  22.     }
  23.  
  24. //call on an event 
  25. function xmlString(xmlFile){
  26.  
  27.     var xmlDoc=loadXMLDoc(xmlFile);
  28.     var root=xmlDoc.documentElement;
  29.     var xml=getElement(root);
  30.     alert(xml);
  31. }
  32.  
  33. //the parsing function: xml to string
  34. function getElement(element){
  35.     var str="";
  36.     if(element.nodeType == 1){
  37.         str="<";
  38.         str+=element.nodeName+" ";
  39.         if(element.hasAttributes()){            
  40.             var atts=element.attributes;
  41.             for(a=0;a<atts.length;a++){
  42.                 var att=atts.item(a);    
  43.                 str+=att.nodeName+"='"+att.nodeValue+"' ";
  44.             }
  45.             str+=">";
  46.         }
  47.         else{
  48.             str+=">";    
  49.         }
  50.  
  51.         if(element.hasChildNodes()){
  52.             var len=element.childNodes.length;            
  53.             for(b=0;b<len;b++){                                
  54.                 //alert(element.childNodes[b].nodeName+' = '+len)
  55.                 str+=getElement(element.childNodes[b]);    //recursion here        
  56.             }
  57.  
  58.         }    
  59.         str+="</"+element.nodeName+">";
  60.     }
  61.     else if(element.nodeType == 3){
  62.         //alert(element.nodeValue)
  63.         str+=element.nodeValue;    
  64.     }
  65.  
  66.     //alert(str);
  67.     return str;
  68. }
above, root element is passed in getElement() function, if it has child elements then iterate all child elements and passed it to getElement() until it gets no child element. Each time getElement() return string (i.e node) which appends to the end of itself.

I'm just a beginner in web-development (only 6-month exp.).
Jan 8 '08 #1
7 8193
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

So what result do you get?
Jan 9 '08 #2
hi, now i got the mistake i was using global variable (b) in for loop. By making it local it works.So here is the working code:
Expand|Select|Wrap|Line Numbers
  1.     function loadXMLDoc(dname)
  2.     {
  3.         var xmlDoc;
  4.         // code for IE
  5.         if (window.ActiveXObject)
  6.         {
  7.             xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  8.         }
  9.         // code for Mozilla, Firefox, Opera, etc.
  10.         else if (document.implementation && document.implementation.createDocument)
  11.         {
  12.             xmlDoc=document.implementation.createDocument("","",null);
  13.         }
  14.         else
  15.         {
  16.             alert('Your browser cannot handle this script');
  17.         }
  18.         xmlDoc.async=false;
  19.         xmlDoc.load(dname);
  20.         return(xmlDoc);
  21.     }
  22.  
  23. function xmlString(file){
  24.     var xmlDoc=loadXMLDoc(file);
  25.     var root=xmlDoc.documentElement;
  26.     var xml=getElement(root);
  27.     //alert(xml);
  28.  
  29.     xml = replace(xml,'<',unescape('&lt;'));
  30.     xml = replace(xml,'>',unescape('&gt;'));
  31.     document.body.innerHTML = "<br /><p>"+xml+"</p><br />";
  32. }
  33.  
  34. function getElement(element){
  35.     var str="";
  36.     if(element.nodeType == 1){
  37.         str="<";
  38.         str+=element.nodeName+" ";
  39.         if(element.hasAttributes()){            
  40.             var atts=element.attributes;
  41.             for(var a=0;a<atts.length;a++){
  42.                 var att=atts.item(a);    
  43.                 str+=att.nodeName+"='"+att.nodeValue+"' ";
  44.             }            
  45.         }        
  46.         str+=">";            
  47.  
  48.         if(element.hasChildNodes()){
  49.             var len=element.childNodes.length;            
  50.             for(var b=0;b<len;b++){                                
  51.                 //alert(element.childNodes[b].nodeName+' = '+len)
  52.                 str+=getElement(element.childNodes[b]);    //recursion here        
  53.             }            
  54.         }        
  55.         str+="</"+element.nodeName+">";
  56.     }
  57.     else if(element.nodeType == 3){
  58.         //alert(element.nodeValue)
  59.         str+=element.nodeValue;    
  60.     }
  61.  
  62.     //alert(str);
  63.     return str;
  64. }
  65.  
  66. //to change "<" n ">" (entities), before print to screen
  67. function replace(string,text,by) {
  68. // Replaces text with by in string
  69.     var strLength = string.length, txtLength = text.length;
  70.     if ((strLength == 0) || (txtLength == 0)) return string;
  71.  
  72.     var i = string.indexOf(text);
  73.     if ((!i) && (text != string.substring(0,txtLength))) return string;
  74.     if (i == -1) return string;
  75.  
  76.     var newstr = string.substring(0,i) + by;
  77.  
  78.     if (i+txtLength < strLength)
  79.         newstr += replace(string.substring(i+txtLength,strLength),text,by);
  80.  
  81.     return newstr;
  82. }
  83.  
//just call xmlString(file) with xml file as parameter. you get xml converted to xml dom's document object and then convert it to xml-string, so i can send it to server, because i can't send javascript object to server (in java/jsp).
here, xml is read from xml file, but in my original code xml is creating in javascript (i.e converting html table structure in xml).
If anybody have any other possibilities or way to do so, please tell me.
Thank you all.
Jan 9 '08 #3
acoder
16,027 Expert Mod 8TB
Glad you got it working and thanks for posting the solution.
//just call xmlString(file) with xml file as parameter. you get xml converted to xml dom's document object and then convert it to xml-string, so i can send it to server, because i can't send javascript object to server (in java/jsp).
here, xml is read from xml file, but in my original code xml is creating in javascript (i.e converting html table structure in xml).
If anybody have any other possibilities or way to do so, please tell me.
Thank you all.
Are you trying to send data in an HTML table to the server-side? What kind of data is it? Can it not be set as form variables?
Jan 9 '08 #4
Glad you got it working and thanks for posting the solution. Are you trying to send data in an HTML table to the server-side? What kind of data is it? Can it not be set as form variables?

I just wanna send html table structure to server.
I'm working on "online movie ticket booking" project.
For theater design I'm using html table, it is totally customizable, so creating/designing table(i.e theater) on some button clicks.
Now I've to send this structure/design to server to store it in xml.

So, I choose to read html table through HTML DOM, and then create a xml (xml dom's document object) using javascript. Now a javascript object can't send on http request, so I've to convert the object to string (i.e xml-string).
Jan 15 '08 #5
acoder
16,027 Expert Mod 8TB
I just wanna send html table structure to server.
I'm working on "online movie ticket booking" project.
For theater design I'm using html table, it is totally customizable, so creating/designing table(i.e theater) on some button clicks.
Now I've to send this structure/design to server to store it in xml.

So, I choose to read html table through HTML DOM, and then create a xml (xml dom's document object) using javascript. Now a javascript object can't send on http request, so I've to convert the object to string (i.e xml-string).
Well, if you wanted an easier solution, you could've just sent the innerHTML of the table. This avoids reading/parsing on the client-side.
Jan 15 '08 #6
Well, if you wanted an easier solution, you could've just sent the innerHTML of the table. This avoids reading/parsing on the client-side.

Ya that's true. Thank you.
Jan 16 '08 #7
acoder
16,027 Expert Mod 8TB
You're welcome. Post again if you have any more questions.
Jan 16 '08 #8

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

Similar topics

4
by: Dan | last post by:
I've encountered some strange behavior in a recursive procedure I'm writing for a bill of materials. First let me ask directly if what I think is happening is even possible: It seems like the...
11
by: Ken | last post by:
Hello, I have a recursive Sierpinski code here. The code is right and every line works fine by itself. I wish for all of them to call the function DrawSierpinski. But in this cae it only calls...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.