1

Hello guys i'm having one hell of a time trying to parse an xml string. The string look like this.

             <sheetData>
                <row r="1" spans="1:12" x14ac:dyDescent="0.25">
                  <c r="A1">
                    <v>1</v>
                  </c>
                  <c r="B1" t="s">
                    <v>3</v>
                  </c>
                  <c r="C1" t="s">
                    <v>2</v>
                  </c>
                  <c r="F1" t="s">
                    <v>0</v>
                  </c>
                  <c r="L1" t="s">
                    <v>1</v>
                  </c>
                </row>
                <row r="2" spans="1:12" x14ac:dyDescent="0.25">
                  <c r="A2">
                    <v>1</v>
                  </c>
                  <c r="B2" t="s">
                    <v>4</v>
                  </c>
                </row>
                <row r="4" spans="1:12" x14ac:dyDescent="0.25">
                  <c r="I4">
                    <v>7</v>
                  </c>
                </row>
              </sheetData>

i have searched and searched but what i keep finding is how to read and xml file using jquery or javascript which do not seem to meet my requirements.

Here is the code i have created and try but i keep failing.

                var len = xmlDoc2.getElementsByTagName("row")[0].childNodes.length;

                  for (var i=0; i < (TotalSheetNodes*len); i++){


                    mysheet.innerHTML +=(xmlDoc2.getElementsByTagName("c")[i].getAttribute("r") ) + "</br>";

                    var v1 = (xmlDoc2.getElementsByTagName("c")[i].getAttribute("r") );


                    /*if the element does not have an attribute of t then add it to then add it to the dictionary*/
                    if (xmlDoc2.getElementsByTagName("c")[i].getAttribute("t") == null)
                    {   
                        var v2 = xmlDoc2.getElementsByTagName("v")[i].childNodes[0].nodeValue

                            Jcell.Dictionary[v1]= v2

                    }
                    //else addid to the sheet element so we can extract the value later  from shared string.
                    else{
                    Jcell.Sheet[(v1)] = v1;

                    }

its failing because of this line.

                var len = xmlDoc2.getElementsByTagName("row")[0].childNodes.length;

so here is my basic question whats the easiest way to get all the child nodes under "row"? In addition i like to make a decision that if the child node has an attribute of "T" i want to assign the value to a different object variable. Your response would be greatly appreciated. I would really like to do this with plain old JavaScript and prefer to stay away from any library's.

Miguel
  • 2,019
  • 4
  • 29
  • 53
  • Did you have a look at related questions? I can see almos half a dozen of questions related to xml parse in javascript. – jap1968 Feb 22 '12 at 18:13
  • as i stated in my question i looked and looked but i just cant get anything to work hence the reason why I'm posting. it appears to me the reading XML FILES and reading XML STRINGS are completely different concepts Thanks for your comment. – Miguel Feb 22 '12 at 18:20
  • Please rename your question since your problems seems not to be "how to parse XML" – Bergi Feb 22 '12 at 18:23
  • I don't understand why? I'm using JavaScript to parse the XML not a server side language? why should i rename the question? – Miguel Feb 22 '12 at 18:26
  • your variable XMLDoc2 seems to hold an already parsed XML document, doesn't it? – Bergi Feb 22 '12 at 18:32
  • it does not hold parsed XML it holds the xml i posted above. the problem is here. var len = xmlDoc2.getElementsByTagName("row")[0].childNodes.length; i have [0] where i need that to increment to the next parent row i try using a for loop but i just keep stumbling on my own two feet. – Miguel Feb 22 '12 at 19:00
  • If it holds an XML *string*, you can't use Node traversal methods. See http://stackoverflow.com/a/8412989/1048572 on how to parse it in plain js – Bergi Feb 22 '12 at 19:27

1 Answers1

1

Are you sure that your xmlDoc2.getElementsByTagName("row") has at least one result? Else, the syntax seems to be fine.

Also, there's much initialisation missing in your script snippet, without which we won't be able to help you.

  • xmlDoc2 - is this an well-parsed XML document object (or an error message?)
  • TotalSheetNodes is what?
  • mysheet seems to be an HTML element?
  • what is JCell? It seems not to be related to the question.

EDIT:

To traverse (seems to be the right word, not "parse") your XML document you should loop through each NodeList on its own. Thats possible, every element inherits the Node interface, not only the xml document object - as you seem to believe.

Also, you should not have to count the rows, as you are only interested in cs. A big mistake seems to be the aritmethic approximation for the number of cs in your (whole) document by multilpying the number or rows with the number of c elements in the first row - your xml source proves this is wrong.

var rows = xmlDoc2.getElementsByTagName("row");
for (var i=0, l=rows.length; i<l; i++) {
    var row = rows[i];
    var cs = row.getElementsByTagName("c");
    for (var j=0, m=cs.length; j<m; j++) {
        var c = cs[j];
        var t = c.getAttribute("t");
        var v = c.getElementsByTagName("v")[0]; // throws an error if there is no "v"

        // do whatever you want with row, c, t and v
    }
}

As you don't use row in your example above, you might just omit the outer loop and use

var cs = xmlDoc2.getElementsByTagName("c");
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • xmlDoc2.getElementsByTagName("row") it absolutely does have values. My problem is that when i encounter a parent element say row i want to read the children under row. It would seem to me that i shouldn't need to do this "var len = xmlDoc2.getElementsByTagName("row")[0].childNodes.length". I would think there is a better method to just identify the parent and loop throught the child nodes. Total sheet nodes is the total row nodes in the xml file. I though i would just put a snippet on here as to not overwhelm anyone with the tons of code. – Miguel Feb 22 '12 at 18:49
  • "TypeError: xmlDoc2.getElementsByTagName("c")[i] is undefined" this is the error im getting. – Miguel Feb 22 '12 at 19:09
  • the problem with the way I'm trying to accomplish the task is that since I'm multiplying the total nodes times the children its trying to read 15 child nodes on the first attempt where there is only 5 child nodes to the parent row node. and the only two child nodes to the second parent node. those this make sense? i need to identify each parent and then just parse the children this is were im failing. – Miguel Feb 22 '12 at 19:13
  • Yeah, I've got your mistake (see my edit). Your error obviosly does not come from the `var len = xmlDoc2.get...`-line. – Bergi Feb 22 '12 at 19:19
  • Thank you for responding to my question this is exaclty what i was trying to acomplish i just could not for more then one reason. I changed your code slightly it appears that this line "for (var j=0; m=cs.length; j – Miguel Feb 22 '12 at 19:40
  • right, that should have been a comma, not a semicolon. edited my answer – Bergi Feb 22 '12 at 19:49