473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

7 2203
acoder
16,027 Expert Mod 8TB
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
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
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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

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

Similar topics

0
by: CJ | last post by:
Can someone look at the code below and tell me whats going on? Here is the problem. I can successfully telnet one of our routers and successfully put in a command in the while statement, but...
2
by: CanoeGuy | last post by:
I have an array that I am populating from a database. I am looping through the array to write the values to a datagrid. At this point, I'm stuck. I can't figure out how to read the first four...
6
by: vasilijepetkovic | last post by:
Hello All, I have a problem with the program that should generate x number of txt files (x is the number of records in the file datafile.txt). Once I execute the program (see below) only one...
1
by: Nicole | last post by:
Hello! I hope there is someone out there who can shed some light on this for me. I have a module that is supposed to look at an access table, pull out each bid record, link to another table to...
5
by: Bruce Lawrence | last post by:
I'm running Access 97 and my modules are looping if someone puts an invalid value in. The setup: 3 macros - get_clock_num, verify_clocknum, append_to_history 3 functions. each in their own...
6
by: Mike | last post by:
Hello, This game will not loop. It compiles, builds, and executes fine, but the game will not loop around no matter what I do. What is wrong with the code? What do I have to add to make it loop...
1
by: Kelvin | last post by:
Hi All, I wrote a program insert new row between rows while the datatable is looping. It display the error message. Exception Details: System.ArgumentException: This row already belongs to this...
2
by: nasirmajor | last post by:
dear all, i have a listbox whose items are displayed in listbox e.g ('1','3','4',) my problem is the last comma (, after 4) i don't want this comma as i have to use this string in sql query....
3
by: Steven Smith | last post by:
Hi everyone, This code makes sense to me, but when I run it, it says "Loop Without Do". Any Suggestions? Thanks, Steven Smith Dim FirstPaymentDate As Date
1
by: Promextheus Xex | last post by:
First php post... Hi there, I'm writing some code for my website to list people listening to my music. I have a loop for an array that dosen't seem to display all people. most of the time it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.