简体   繁体   中英

parse XML string using javascript

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.

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 row s, as you are only interested in c s. A big mistake seems to be the aritmethic approximation for the number of c s 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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM