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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samelzoro
    New Member
    • Jan 2008
    • 4

    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)

    [CODE=javascript] //load a xml
    function loadXMLDoc(dnam e)
    {
    var xmlDoc;
    // code for IE
    if (window.ActiveX Object)
    {
    xmlDoc=new ActiveXObject(" Microsoft.XMLDO M");
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.imple mentation && document.implem entation.create Document)
    {
    xmlDoc=document .implementation .createDocument ("","",null) ;
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    xmlDoc.async=fa lse;
    xmlDoc.load(dna me);
    return(xmlDoc);
    }

    //call on an event
    function xmlString(xmlFi le){

    var xmlDoc=loadXMLD oc(xmlFile);
    var root=xmlDoc.doc umentElement;
    var xml=getElement( root);
    alert(xml);
    }

    //the parsing function: xml to string
    function getElement(elem ent){
    var str="";
    if(element.node Type == 1){
    str="<";
    str+=element.no deName+" ";
    if(element.hasA ttributes()){
    var atts=element.at tributes;
    for(a=0;a<atts. length;a++){
    var att=atts.item(a );
    str+=att.nodeNa me+"='"+att.nod eValue+"' ";
    }
    str+=">";
    }
    else{
    str+=">";
    }

    if(element.hasC hildNodes()){
    var len=element.chi ldNodes.length;
    for(b=0;b<len;b ++){
    //alert(element.c hildNodes[b].nodeName+' = '+len)
    str+=getElement (element.childN odes[b]); //recursion here
    }

    }
    str+="</"+element.nodeN ame+">";
    }
    else if(element.node Type == 3){
    //alert(element.n odeValue)
    str+=element.no deValue;
    }

    //alert(str);
    return str;
    }[/CODE]

    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.).
    Last edited by acoder; Jan 8 '08, 04:33 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    So what result do you get?

    Comment

    • samelzoro
      New Member
      • Jan 2008
      • 4

      #3
      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:
      [CODE=javascript]
      function loadXMLDoc(dnam e)
      {
      var xmlDoc;
      // code for IE
      if (window.ActiveX Object)
      {
      xmlDoc=new ActiveXObject(" Microsoft.XMLDO M");
      }
      // code for Mozilla, Firefox, Opera, etc.
      else if (document.imple mentation && document.implem entation.create Document)
      {
      xmlDoc=document .implementation .createDocument ("","",null) ;
      }
      else
      {
      alert('Your browser cannot handle this script');
      }
      xmlDoc.async=fa lse;
      xmlDoc.load(dna me);
      return(xmlDoc);
      }

      function xmlString(file) {
      var xmlDoc=loadXMLD oc(file);
      var root=xmlDoc.doc umentElement;
      var xml=getElement( root);
      //alert(xml);

      xml = replace(xml,'<' ,unescape('&lt; '));
      xml = replace(xml,'>' ,unescape('&gt; '));
      document.body.i nnerHTML = "<br /><p>"+xml+"</p><br />";
      }

      function getElement(elem ent){
      var str="";
      if(element.node Type == 1){
      str="<";
      str+=element.no deName+" ";
      if(element.hasA ttributes()){
      var atts=element.at tributes;
      for(var a=0;a<atts.leng th;a++){
      var att=atts.item(a );
      str+=att.nodeNa me+"='"+att.nod eValue+"' ";
      }
      }
      str+=">";

      if(element.hasC hildNodes()){
      var len=element.chi ldNodes.length;
      for(var b=0;b<len;b++){
      //alert(element.c hildNodes[b].nodeName+' = '+len)
      str+=getElement (element.childN odes[b]); //recursion here
      }
      }
      str+="</"+element.nodeN ame+">";
      }
      else if(element.node Type == 3){
      //alert(element.n odeValue)
      str+=element.no deValue;
      }

      //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.substrin g(0,txtLength)) ) return string;
      if (i == -1) return string;

      var newstr = string.substrin g(0,i) + by;

      if (i+txtLength < strLength)
      newstr += replace(string. substring(i+txt Length,strLengt h),text,by);

      return newstr;
      }
      [/CODE]

      //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.
      Last edited by acoder; Jan 9 '08, 02:48 PM. Reason: Added code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Glad you got it working and thanks for posting the solution.
        Originally posted by samelzoro
        //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?

        Comment

        • samelzoro
          New Member
          • Jan 2008
          • 4

          #5
          Code

          Originally posted by acoder
          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).
          Last edited by samelzoro; Jan 15 '08, 07:02 AM. Reason: to add more info

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by samelzoro
            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.

            Comment

            • samelzoro
              New Member
              • Jan 2008
              • 4

              #7
              Originally posted by acoder
              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.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You're welcome. Post again if you have any more questions.

                Comment

                Working...