By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

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

P: 4
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
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

So what result do you get?
Jan 9 '08 #2

P: 4
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
Expert Mod 15k+
P: 16,027
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

P: 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
Expert Mod 15k+
P: 16,027
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

P: 4
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
Expert Mod 15k+
P: 16,027
You're welcome. Post again if you have any more questions.
Jan 16 '08 #8

Post your reply

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