By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,978 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

looping problem: get distinct values for one person from multiple entries in XML file

P: 5
Hello everyone,

I want to make a page that select distinct values for one person from an XML file into a table. The XML has multiple entry's for this person. I can get all of the data, or just the first one, but I want all of one person, represented by his or hers "INLOGKODE". I try it with a loop, while a condition is true. It just does n't work this way I guess...

Here's what I've got so far:

The XML:( History.xml on the server uploaded)
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <dataroot>
  3. <History>
  4. <INLOGKODE>Beer4</INLOGKODE>
  5. <DATUM>2011-04-18T00:00:00</DATUM>
  6. <SCORE>38</SCORE>
  7. <HCP-OUD>21.5</HCP-OUD>
  8. <HCP-NIEUW>20.7</HCP-NIEUW>
  9. <OPMERKING>Damesmiddag</OPMERKING>
  10. </History>
  11. <History>
  12. <INLOGKODE>Beer4</INLOGKODE>
  13. <DATUM>2011-05-17T00:00:00</DATUM>
  14. <SCORE>32</SCORE>
  15. <HCP-OUD>21</HCP-OUD>
  16. <HCP-NIEUW>21</HCP-NIEUW>
  17. <OPMERKING>ESO2011 gp1 dag2 Vnd</OPMERKING>
  18. </History>
  19. <History>
  20. <INLOGKODE>Beer4</INLOGKODE>
  21. <DATUM>2011-05-20T00:00:00</DATUM>
  22. <SCORE>19</SCORE>
  23. <HCP-OUD>21.1</HCP-OUD>
  24. <HCP-NIEUW>21.2</HCP-NIEUW>
  25. <OPMERKING>ESO2011 finale</OPMERKING>
  26. </History>
  27. <History>
  28. <INLOGKODE>Trox214</INLOGKODE>
  29. <DATUM>2011-05-08T00:00:00</DATUM>
  30. <SCORE>31</SCORE>
  31. <HCP-OUD>17.3</HCP-OUD>
  32. <HCP-NIEUW>17.4</HCP-NIEUW>
  33. <OPMERKING>Maandbeker2011 mei</OPMERKING>
  34. </History>
  35. <History>
  36. <INLOGKODE>Trox214</INLOGKODE>
  37. <DATUM>2011-07-05T00:00:00</DATUM>
  38. <SCORE>34</SCORE>
  39. <HCP-OUD>17.8</HCP-OUD>
  40. <HCP-NIEUW>17.8</HCP-NIEUW>
  41. <OPMERKING>Herenmiddag</OPMERKING>
  42. </History>
  43. <History>
  44. <INLOGKODE>Trox214</INLOGKODE>
  45. <DATUM>2011-05-10T00:00:00</DATUM>
  46. <SCORE>34</SCORE>
  47. <HCP-OUD>17.4</HCP-OUD>
  48. <HCP-NIEUW>17.4</HCP-NIEUW>
  49. <OPMERKING>Herenmiddag</OPMERKING>
  50. </History>
  51. <History>
  52. <INLOGKODE>Trox214</INLOGKODE>
  53. <DATUM>2011-04-02T00:00:00</DATUM>
  54. <SCORE>23</SCORE>
  55. <HCP-OUD>16.9</HCP-OUD>
  56. <HCP-NIEUW>17</HCP-NIEUW>
  57. <OPMERKING>Maandbeker2011 april</OPMERKING>
  58. </History>
  59. <History>
  60. <INLOGKODE>Trox214</INLOGKODE>
  61. <DATUM>2011-05-31T00:00:00</DATUM>
  62. <SCORE>33</SCORE>
  63. <HCP-OUD>17.4</HCP-OUD>
  64. <HCP-NIEUW>17.4</HCP-NIEUW>
  65. <OPMERKING>Herenmiddag</OPMERKING>
  66. </History>
  67. </dataroot>
  68.  
The site is in Wordpress, I extract the currently logged in user in PHP and then pass it to Javascript on the page:

Expand|Select|Wrap|Line Numbers
  1. <?php session_start();
  2. $_SESSION["user_login"] = $DataArray[2];
  3. $_SESSION["display_name"] = $DataArray[3];
  4. require_once( '../../../../wp-load.php' );
  5. global $current_user;
  6. get_currentuserinfo();
  7. // A welcoming (in Dutch)
  8.       echo 'Welkom '.$current_user->display_name . '. Uw inlogkode is: '.$current_user->user_login. "\n";
  9. ?>
  10.  
  11. <script type="text/javascript"> // The approach of the data XML//
  12. if (window.XMLHttpRequest)
  13.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  14.   xmlhttp=new XMLHttpRequest();
  15.   }
  16. else
  17.   {// code for IE6, IE5
  18.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  19.   }
  20. xmlhttp.open("GET","History.xml",false);
  21. xmlhttp.send();
  22. xmlDoc=xmlhttp.responseXML; 
  23.  
  24. document.write("<table border='1'>");
  25. var x=xmlDoc.getElementsByTagName("History");
  26. var persoon =new Object();
  27. var persoon = "<?php echo  $current_user->user_login ?>";
  28.  
  29. with ({xmlDoc.getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue}); == persoon);
  30.  
  31.   { 
  32.   document.write("<tr><td>");
  33.   document.write(xmlDoc.getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
  34.   document.write("</td><td>");
  35.   document.write(xmlDoc.getElementsByTagName("SCORE")[0].childNodes[0].nodeValue);
  36.   document.write("</td><td>");
  37.   document.write(xmlDoc.getElementsByTagName("HCP-OUD")[0].childNodes[0].nodeValue);
  38.   document.write("</td><td>");
  39.   document.write(xmlDoc.getElementsByTagName("HCP-NIEUW")[0].childNodes[0].nodeValue);
  40.   document.write("</td><td>");
  41.   document.write(xmlDoc.getElementsByTagName("OPMERKING")[0].childNodes[0].nodeValue);
  42.   document.write("</td></tr>");
  43.   }
  44. document.write("</table>");
  45. </script>
  46.  
I tried this "with" loop, but get an error:
missing : after property id

Anyone any idea? Or maybe just a wrong method for what I want? Some help would be appreciated..
Nov 7 '11 #1

✓ answered by acoder

There's no need for the second for loop.

Once you find a match, you just need to get the data for that "History" element, i.e.
Expand|Select|Wrap|Line Numbers
  1. history[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue

Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
You could use a while loop or a for loop. Your syntax is incorrect.

There's different ways you could do this. One way would be to loop over the INLOGKODEs, compare its values and then get the corresponding data values, e.g.
Expand|Select|Wrap|Line Numbers
  1. // first get the elements, e.g.
  2. var inlogkodes = xmlDoc.getElementsByTagName("INLOGKODE");
  3. for (i = 0; i < inlogkodes.length; i++) {
  4.     // check value against persoon
  5.     ...
  6.     // if value matches, then get the ith index of the data, e.g.
  7.     datum[i]
Also, try to avoid document.write(), though in this case, it's a synchronous request. It does beg the question why you don't just use PHP to parse and display...
Nov 9 '11 #2

P: 5
Thank you acoder for looking into my silly scripting attempts.

Aside from your other questions, which I will answer in another post, here's what I've come up to for now:

Expand|Select|Wrap|Line Numbers
  1.  
  2. var inlogkode = "<?php echo  $current_user->user_login ?>";
  3. var persoon = xmlDoc.getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue;
  4.  
  5.  
  6.   for (i=0;i<persoon.length;i++) // The iteration process
  7.   if ( persoon == inlogkode )    // Checking the value of array against current user
  8.  
  9.                                  // If so, write value's to the table..
  10.  
  11.   { 
  12.   document.write("<tr><td>");
  13.   document.write(x[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
  14. // and so on...
  15.  
  16.  
  17.  
The code doesn't give any value's . From Firebug I can see the XML is loaded, variables are defined, but nothing happens in the writing value process.

Is it anything with the iteration process? Or can't I define the condition like the way I did?

Appreciate any further tips or hints.
Nov 11 '11 #3

P: 5
I 've read about avoiding document.write(), but don't fully understand the reason of that warning. All tutorials I find use it and most snippets do the work...

Anyone with a good introduction on this subject? Please feel free!

Than the other one: Why not use PHP? Just because I feel more at ease with Jscript. Though not very experienced with both, Jscript was just more "readable" to me. Maybe some other time, without the urge to just get it done as soon as possible without too many diving into it. Although I feel that alone for performance reasons server side will be the better option. Maybe later I will read some more about PHP and SQL and rewrite the whole into more efficient coding.

Any help is welcome...thanks in advance!
Nov 11 '11 #4

P: 5
After reading previous attempts, it is now obvious to me why the loop didn't give any result.

So I tried this nested loop: a for.. loop with a conditional "if .. else" inside.

Helas, no result either. So where do I go wrong?

Fragment of the script:

Expand|Select|Wrap|Line Numbers
  1. .......
  2. var x=xmlDoc.getElementsByTagName("History");
  3. var persoon = "<?php echo  $current_user->user_login ?>";
  4. var inlogkode = xmlDoc.getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue;
  5.  
  6. for (i = 1; i < inlogkode.length; i++)                //loop through the XMLdoc//
  7.     {
  8.   inlogkode =(x[i].getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue); 
  9.  
  10. if ( inlogkode == persoon )  //See if value corresponds to current user//
  11.  
  12.   {                          //if so, write the table //
  13.  
  14. for (j = 0; j < inlogkode.length; j++)      
  15.  
  16.   document.write("<tr><td>"); 
  17.   document.write(x[j].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
  18. // and so on..
  19.  
  20.   }
  21.   else                        // if not, return to the first loop and increment i //
  22.   {
  23.   (i=i+1)
  24.   }
  25.  
  26.   }
  27.  
  28.  
Nov 15 '11 #5

acoder
Expert Mod 15k+
P: 16,027
On line 2, call "x", "history" to make things clearer.

On line 6, you need to loop over this history object, not inlogkode. Also, you won't need line 4. Any reason why the loop index starts from 1 and not 0?

You also won't need lines 22-24 - that's the job of the i++ in the for loop declaration.

Test/debug your code with breakpoints, watching variables and stepping through it line by line using, e.g. Firebug
Nov 17 '11 #6

P: 5
Thank You, Acoder!

These tips helped me quite further. I get values from the current user, so far, so good.
But, it spits out the table quite odd: The date does not get formatted into the first row of the table!

Here a small example of the first three rows:
Welkom Luc Trox. Uw inlogkode is: Trox214
2011-05-08T00:00:002011-07-05T00:00:002011-05-10T00:00:00
31 17.3 17.4 Maandbeker2011 mei
34 17.8 17.8 Herenmiddag
34 17.4 17.4 Herenmiddag
Here's the script from the table definition on:

Expand|Select|Wrap|Line Numbers
  1. document.write("<table border='1'>");
  2. var history=xmlDoc.getElementsByTagName("History");
  3. var persoon = "<?php echo  $current_user->user_login ?>";
  4. for (i = 0; i < history.length; i++) 
  5.   {
  6.   inlogkode =(history[i].getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue);
  7.  
  8.    if ( inlogkode == persoon )                       // Here the control
  9.  
  10.     {
  11.  
  12. for (j = i; j < inlogkode.length; j++)                // This did the trick, get them from the ith index number
  13.  
  14.   document.write("<tr><td>");
  15.   document.write(history[j].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
  16.   document.write("</td><td>");
  17.   document.write(history[j].getElementsByTagName("SCORE")[0].childNodes[0].nodeValue);
  18.   document.write("</td><td>");
  19.   document.write(history[j].getElementsByTagName("HCP-OUD")[0].childNodes[0].nodeValue);
  20.   document.write("</td><td>");
  21.   document.write(history[j].getElementsByTagName("HCP-NIEUW")[0].childNodes[0].nodeValue);
  22.   document.write("</td><td>");
  23.   document.write(history[j].getElementsByTagName("OPMERKING")[0].childNodes[0].nodeValue);
  24.   document.write("</td></tr>");
  25.      }   
  26.    } 
  27.  
  28.   document.write("</table>"); 
  29.  
Why the odd behavior? Or better abandon the "document.write" and use something like this?

Expand|Select|Wrap|Line Numbers
  1.  {
  2.       txt=txt + "<tr>";
  3.  
  4.         xx=x[i].getElementsByTagName("DATUM");
  5.         {
  6.         try
  7.           {
  8.           txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
  9.           }                    
  10.                                               //and so on
  11.  
I think with some more help I will be able to get it right, please some more help on this.
Nov 17 '11 #7

acoder
Expert Mod 15k+
P: 16,027
There's no need for the second for loop.

Once you find a match, you just need to get the data for that "History" element, i.e.
Expand|Select|Wrap|Line Numbers
  1. history[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue
Nov 18 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.