M
M
mopah2016-12-02 13:30:34
JavaScript
mopah, 2016-12-02 13:30:34

Parsing XML file. Why can't I see child elements?

Hello, I have a problem that I can't solve. I parse the XML file. With the help of Ajax I take the file, I start to transform it with the help of jQuery. XML file structure example:

<Row>
    <Cell ss:StyleID="s158"><Data ss:Type="String">Номер товара</Data></Cell>
    <Cell ss:StyleID="s160" ss:HRef="http://besttea.ru/smola-puer-shu-v-poroshke/"
     x:HRefScreenTip="Объем"><Data ss:Type="String"><b>Название</b></Data></Cell>
    <Cell ss:StyleID="s162"><Data ss:Type="String"><b>Объем</b></Data><Comment
      ss:Author="Е.М"><ss:Data xmlns="none"><B><Font
         html:Face="Tahoma" x:CharSet="204" html:Size="9" html:Color="#000000"><b>Описание</b></Font></B></ss:Data></Comment></Cell>
    <Cell ss:StyleID="s158"><Data ss:Type="String">Акция!!!</Data><NamedCell
      ss:Name="_FilterDatabase"/></Cell>
    <Cell ss:StyleID="s163"><Data ss:Type="Number">400</Data></Cell>
    <Cell ss:StyleID="s164"/>
    <Cell ss:StyleID="s164" ss:Formula="=RC[-1]*RC[-2]"><Data ss:Type="Number">0</Data></Cell>
   </Row>

So, we have a lot of rows ( ROW tag), they have CELL cells. Some cells have DATA and COMMENT, I want to select them as separate tags, it's convenient. The script is like this:
jQuery(document).ready(function() {
  var array = [];
  var arrayHTML = [];
  jQuery(".transform").click(
    function() {
      jQuery.ajax({
        type: "GET",
        url: "путь к файлу",
        dataType: "xml",
        error: function() {
          console.log("Не вышло");
        },
        success: function(temp) {

          var got = jQuery(temp).find("Row");
          var maxL = 0;
          for (i=0; i<got.length; i++) { // Перебор строк

            var got2 = jQuery(got[i]).find("Cell");
            var length2 = got2.length; //кол-во ячеек Cell в каждом Row
            array[i] = [];
            for (j=0; j<length2; j++) { // Перебор ячеек
              var got31 = jQuery(got2[j]).find("Data").textContent; 
              var got32 = jQuery(got2[j]).find("Comment").textContent;
              if (got31 && got32) { //если есть и Data и Comment
                array[i][j] = got31;
                j++;
                length2; // увеличиваем дилну на 1, т.к. появилась доп. ячейка за счет наличия комментария
                array[i][j] = got32;
              }
              else {
                array[i][j] = got2[j].textContent;
              }
              if (maxL < length2) {
                maxL = length2;
              }
              console.log("i="+ i + " " + "j=" + j + " " + "значение =" + array[i,j]);
            }

          }
          console.log("конец");
        }
      });
    }
  );
});

Everything works as it should, but it is not possible to select Data and Comment separately, as if he does not see them. In the console, I tried to select them this way and that, but nothing. Screenshots from the console:
So I looked, there seem to be child elements, and there are 2 of them:
d0ac011303a44d2f81aea7f15b10fcbd.jpg
Ok. I select, for example, the Data element (both children and find give the same result):
f8fd457e9dd4453f949c9bc9862e2ab2.jpg
And I see that in the textContent field there is a merger of two Data and Comment elements, albeit separated by a line break (the Data element should only refer to "packaging (20gr)" , and everything else to Comment).
Another nuance, when I select Cell in the console, the property tree is also signed as [Cell], but when choosing Data or Comment, we see that the tree is marked as []:
c52c67531b6a4ff1afc0627acf82c44a.jpg

I immediately apologize for some tongue-tied language, I'm just studying JS.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question