Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2013, 04:28 PM   PM User | #1
franrtorres77
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
franrtorres77 is an unknown quantity at this point
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.
franrtorres77 is offline   Reply With Quote
Old 03-06-2013, 08:43 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
__________________
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.

Last edited by Old Pedant; 03-06-2013 at 08:45 PM..
Old Pedant is offline   Reply With Quote
Old 03-06-2013, 11:27 PM   PM User | #3
franrtorres77
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
franrtorres77 is an unknown quantity at this point
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>");
   }
franrtorres77 is offline   Reply With Quote
Old 03-07-2013, 12:34 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 03-07-2013, 12:37 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
childnodes, xml, xml parse

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:43 PM.


Advertisement
Log in to turn off these ads.