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) - //load a xml
-
function loadXMLDoc(dname)
-
{
-
var xmlDoc;
-
// code for IE
-
if (window.ActiveXObject)
-
{
-
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
-
}
-
// code for Mozilla, Firefox, Opera, etc.
-
else if (document.implementation && document.implementation.createDocument)
-
{
-
xmlDoc=document.implementation.createDocument("","",null);
-
}
-
else
-
{
-
alert('Your browser cannot handle this script');
-
}
-
xmlDoc.async=false;
-
xmlDoc.load(dname);
-
return(xmlDoc);
-
}
-
-
//call on an event
-
function xmlString(xmlFile){
-
-
var xmlDoc=loadXMLDoc(xmlFile);
-
var root=xmlDoc.documentElement;
-
var xml=getElement(root);
-
alert(xml);
-
}
-
-
//the parsing function: xml to string
-
function getElement(element){
-
var str="";
-
if(element.nodeType == 1){
-
str="<";
-
str+=element.nodeName+" ";
-
if(element.hasAttributes()){
-
var atts=element.attributes;
-
for(a=0;a<atts.length;a++){
-
var att=atts.item(a);
-
str+=att.nodeName+"='"+att.nodeValue+"' ";
-
}
-
str+=">";
-
}
-
else{
-
str+=">";
-
}
-
-
if(element.hasChildNodes()){
-
var len=element.childNodes.length;
-
for(b=0;b<len;b++){
-
//alert(element.childNodes[b].nodeName+' = '+len)
-
str+=getElement(element.childNodes[b]); //recursion here
-
}
-
-
}
-
str+="</"+element.nodeName+">";
-
}
-
else if(element.nodeType == 3){
-
//alert(element.nodeValue)
-
str+=element.nodeValue;
-
}
-
-
//alert(str);
-
return str;
-
}
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.).
7 8193
Welcome to TSDN!
So what result do you get?
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: -
function loadXMLDoc(dname)
-
{
-
var xmlDoc;
-
// code for IE
-
if (window.ActiveXObject)
-
{
-
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
-
}
-
// code for Mozilla, Firefox, Opera, etc.
-
else if (document.implementation && document.implementation.createDocument)
-
{
-
xmlDoc=document.implementation.createDocument("","",null);
-
}
-
else
-
{
-
alert('Your browser cannot handle this script');
-
}
-
xmlDoc.async=false;
-
xmlDoc.load(dname);
-
return(xmlDoc);
-
}
-
-
function xmlString(file){
-
var xmlDoc=loadXMLDoc(file);
-
var root=xmlDoc.documentElement;
-
var xml=getElement(root);
-
//alert(xml);
-
-
xml = replace(xml,'<',unescape('<'));
-
xml = replace(xml,'>',unescape('>'));
-
document.body.innerHTML = "<br /><p>"+xml+"</p><br />";
-
}
-
-
function getElement(element){
-
var str="";
-
if(element.nodeType == 1){
-
str="<";
-
str+=element.nodeName+" ";
-
if(element.hasAttributes()){
-
var atts=element.attributes;
-
for(var a=0;a<atts.length;a++){
-
var att=atts.item(a);
-
str+=att.nodeName+"='"+att.nodeValue+"' ";
-
}
-
}
-
str+=">";
-
-
if(element.hasChildNodes()){
-
var len=element.childNodes.length;
-
for(var b=0;b<len;b++){
-
//alert(element.childNodes[b].nodeName+' = '+len)
-
str+=getElement(element.childNodes[b]); //recursion here
-
}
-
}
-
str+="</"+element.nodeName+">";
-
}
-
else if(element.nodeType == 3){
-
//alert(element.nodeValue)
-
str+=element.nodeValue;
-
}
-
-
//alert(str);
-
return str;
-
}
-
-
//to change "<" n ">" (entities), before print to screen
-
function replace(string,text,by) {
-
// Replaces text with by in string
-
var strLength = string.length, txtLength = text.length;
-
if ((strLength == 0) || (txtLength == 0)) return string;
-
-
var i = string.indexOf(text);
-
if ((!i) && (text != string.substring(0,txtLength))) return string;
-
if (i == -1) return string;
-
-
var newstr = string.substring(0,i) + by;
-
-
if (i+txtLength < strLength)
-
newstr += replace(string.substring(i+txtLength,strLength),text,by);
-
-
return newstr;
-
}
-
//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.
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?
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).
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.
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.
You're welcome. Post again if you have any more questions.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;
|
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...
...
|
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...
|
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...
|
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...
|
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...
|
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?...
|
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 ??
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |