Pasoum
Pasoum

Reputation: 27

I want to export any xml file to excel file (xls or tab delimited)

I want to export any xml file to excel (xls or tab delimited). I am using Jquery to open the xml. I created a recursive function that is testing whether a node has children or not and this is where I am stuck. An example of what I would want:

<form1>
    <Q1-2>
        <division>Tony</division>
        <branch>Sailer Branch</branch>
    </Q1-2>
    <Q3>
        <moreServBusFunctProg>
            <Row2>
                <servFunctProgr>Awesome Service</servFunctProgr>
            </Row2>
            <Row3>
                <servFunctProgr>Whatever you want</servFunctProgr>
            </Row3>
            <Row4>
                <servFunctProgr>Service with quality</servFunctProgr>
            </Row4>
            <Row5>
                <servFunctProgr>Everything</servFunctProgr>
            </Row5>
            <Row6>
                <servFunctProgr>Function 1</servFunctProgr>
            </Row6>
        </moreServBusFunctProg>
    </Q3>
</form1>

I would want to retrieve:

----------------------------------------------------------------------------------------
| division | branch        | servFunctProgr  | servFunctProgr    | servFunctProgr       |
----------------------------------------------------------------------------------------
| Tony     | Sailer Branch | Awesome Service | Whatever you want | Service with quality |
----------------------------------------------------------------------------------------

Now I am sharing my Recursive function

function showTags(jqueryObject,text,tags) 
{
  if (jqueryObject.length === 0)
  {
    return;
  }
  jqueryObject.children().each(function() {
    tags += "\t" + this.nodeName.toLowerCase();
    text += $(jqueryObject).contents().text() + "\t" ;
    showTags($(this),text, tags);
    alert(tags);
  });

  //alert(text);
}

Upvotes: 0

Views: 1463

Answers (1)

Pasoum
Pasoum

Reputation: 27

I solved my problem in case someone else is trying to do the same. First of all, I had to create an object method so I could keep the current values of my variables whenever I pass them as parameters to the recursive function.

function result(txt, tgs, i) {
                    this.text = txt
                    this.tags = tgs
                    this.i = i
                }

Secondly, I stored the text from file file as a string into a variable named allText. Then I created and initialized some variables, before calling my recursive function:

var allText = $('#editor').text();
i = 0;
    txt = "";
    tags = "";
    rs = new result(txt,tags,i);
    showTags($(allText),rs)

Finally, I created my recursive function:

function showTags(jqueryObject,rs) {

                    tempTag = "";
                    tag = new Array();
                    tempTst = "";
                    tst = new Array();
                    firstLine = "";
                    secondLine = "";

                    if (jqueryObject.children().length === 0)
                    {

                        //alert(" in last");
                        return;
                    }
                    else
                    {
                        jqueryObject.children().each(function() {
                            //alert("J suis au else");
                            tempTag = this.nodeName.toLowerCase();
                            //alert(tempTag);
                            if($(this).children().length ===0)
                            {
                                $(jqueryObject).find(tempTag).each(function(){
                                    rs.text +=($(this).contents().eq(0).text() +"\t");
                                    rs.tags +=(tempTag + "\t");
                                    //alert("<" + tag [i] + ">" + tst [i] + "</" + tag [i] + ">");
                                    (rs.i)++;
                                    //alert(rs.tags + "\n" + rs.text);
                                });
                            }
                            else
                            {
                                showTags($(this),rs);
                            }
                        });
                    }

                }

And, Yes it works. Now I only retrieve the nodes that has text a children and the text in the nodes. What I did at the end is creating a new tab-delimited file where I paste all the xml tags retrieved on the first line and all their content on the second line. Boom I have my xml converted to a tab-delimited file I can open with excel.

Upvotes: 1

Related Questions