Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parse xml with sub child nodes

    Hello

    I have the next xml file with sub childs, and I want to read them for populate the data into a table. Right now, I know how to do it in a simple way, with childnodes, but I really dont know how to achieve it for getting the sub childnodes.

    Code:
      <Result>
        <Record id="000231">
        <AC_NPD/>
        <Name>Company1</Name>
        <AC_CPR>00003</AC_CPR>
        <AC_ALM>00</AC_ALM>
        <AC_FEC>12/01/2007</AC_FEC>
        <AC_LNA ncols="6">
           <Row>
              <Column>000084</Column>
              <Column>1.230</Column>
              <Column/>
              <Column/>
              <Column/>
              <Column/>
           </Row>
        </AC_LNA>
        <AC_FSE>12/01/2007</AC_FSE>
        <AC_AV/>
        <AC_UFH ncols="3"/>
        </Record>
        </Result>
    Now, I paint the table with the results, using this script:

    Code:
         <script>
         if (window.XMLHttpRequest)
         {// code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
          }
        else
         {// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
    
         xmlhttp.open("GET","xml/pruebas/resp2.asp",false);
         xmlhttp.send();
         xmlDoc=xmlhttp.responseXML; 
    
    
         document.write("<table class='table table-striped table-condensed table-bordered'  id='example'>");
        document.write("<thead>");
            document.write("<tr class='odd gradeX'>");
    	  document.write("<th>Sale</th>");
            document.write("<th>Name</th>");
            document.write("<th>Date</th>");
    		document.write("<th>Date Sale</th>");
    		document.write("<th>Item</th>");
    		document.write("<th>Quantity</th>");
    		document.write("<th class='hidden-phone'>Price</th>");
    		document.write("<th class='hidden-phone'>Total</th>");
    		document.write("<th>Sale Item</th>");
    		document.write("<th>Button</th>");
           document.write("</tr>");
           document.write(" </thead>");
        var x=xmlDoc.getElementsByTagName("Record");
    
        for (i=0;i<x.length;i++)
         { 
      
      
         document.write("<tr>");
         document.write("<td>");  document.write("</td>");
         document.write("<td>");  document.write(x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue);  document.write("</td>");
         document.write("<td>");  document.write(x[i].getElementsByTagName("AC_FEC")[0].childNodes[0].nodeValue);  document.write("</td>");
         document.write("<td>");  document.write(x[i].getElementsByTagName("AC_FSE")[0].childNodes[0].nodeValue);  document.write("</td>");
         document.write("<td>"); document.write(x[i].getElementsByTagName("AC_LNA")[0].childNodes[0].nodeValue);   document.write("</td>");
         document.write("<td>");   document.write("</td>");
         document.write("<td>");   document.write("</td>");
         document.write("<td>");   document.write("</td>");
         document.write("<td>");  document.write("</td>");
    
    
        document.write("<td> <a data-toggle='modal' class='btn' href='sale.asp?&number=");     document.write(x[i].getElementsByTagName("AC_FEC")[0].childNodes[0].nodeValue);    document.write("' data-target='#myModal'>  My Sale  </a> ");                document.write("   </td>");
    
         document.write("</tr>");
         }
        document.write("</table>");
        </script>
    So, if anyone knows how to get the sub child node i would be very gratefully.

    Best regards.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ugh ugh ugh, and double-ugh.

    Has nobody told you that document.write() is very very obsolete?

    You *REALLY* should be doing all this with DOM methods, not document.write.

    And not to ask a really dumb question, but...

    Since you are fetching the date from xml/pruebas/resp2.asp, why not do all the XML parsing and table creation in your ASP code? Response.Write in ASP is *NOT* obsolete. (Well, no more so than ASP itself is.)

    Anyway, I assume by sub-child nodes you are referring to the <Column> tags in this segment:
    Code:
        <AC_LNA ncols="6">
           <Row>
              <Column>000084</Column>
              <Column>1.230</Column>
              <Column/>
              <Column/>
              <Column/>
              <Column/>
           </Row>
        </AC_LNA>
    ???

    If so, it's pretty easy:
    Code:
        var ac_lna = x[i].getElementsByTagName("AC_LNA");
        var columns = ac_lna.getElementsByTagName("Column");
        for ( var c = 0; c < columns.length; ++c )
        {
            var column = columns[c];
            ... do what you will with column ...
        }
    In other words, just repeat the same kind of code that got you the <Record> tags.

    Note that you could get the <Row> tag inside of <AC_LNA> and then get the <Column> tags, but it's really not needed. getElementsByTagName() will skip over intermediate nodes to find all matches for you.
    Last edited by Old Pedant; 03-06-2013 at 08:45 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hello, thank you for answering, I m just a beginner with XML and don't know about the Dom way. anyway, I have tried the code you submitted but I get the next error:

    object <NodeList> has no method 'getElementsByTagName'

    and this is the code:

    Code:
    var ac_lna = x[i].getElementsByTagName("AC_LNA");
       var columns = ac_lna.getElementsByTagName("Column");
        
       for ( var c = 0; c < columns.length; ++c )
       {
           var column = columns[c];
           document.write("<td>");  document.write(column[c].getElementsByTagName("Column")[0].childNodes[0].nodeValue);  document.write("</td>");
       }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Whoops. My goof. Missed the [0] on first line there:
    Code:
        var ac_lna = x[i].getElementsByTagName("AC_LNA")[0];
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    But then this is wrong:
    Code:
    document.write("<td>");  document.write(column[c].getElementsByTagName("Column")[0].childNodes[0].nodeValue);  document.write("</td>");
    column will be a *SINGLE* <Column> tag. You can't use [c] on it.

    And why do you break it up into separate document.write calls????

    Code:
    document.write("<td>" + column.nodeValue + "</td>");
    I think that works.

    But really, it's way way past time to move past document.write!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •