Loading...
Logo
Processing Forum
Hi!

I thought I had cracked it but have a last minute problem to solve.  I will keep trying but in the meantime, I wondered if anyone had a quick solution after tackling this before.

Here is the XML:-

Copy code
  1. <product_lines>
  2. <number_ranges id="2" />
  3. <products>
  4. <watch id="1" range="1" filename="range1watch1.jpg" description="Description 1" />
  5. <watch id="2" range="1" filename="range1watch2.jpg" description="Description 2" />
  6. <watch id="3" range="1" filename="range1watch3.jpg" description="Description 3" />
  7. <watch id="5" range="1" filename="range1watch5.jpg" description="Description 5" />
  8. <watch id="6" range="1" filename="range1watch6.jpg" description="Description 6" />
  9. <watch id="7" range="1" filename="range1watch7.jpg" description="Description 7" />
  10. <watch id="8" range="1" filename="range1watch8.jpg" description="Description 8" />
  11. <watch id="9" range="2" filename="range2watch1.jpg" description="Description 1" />
  12. <watch id="10" range="2" filename="range2watch2.jpg" description="Description 2" />
  13. <watch id="11" range="2" filename="range2watch3.jpg" description="Description 3" />
  14. </products>
  15. </product_lines>

What I would like to do is parse only the watch children that are a certain range.  I have tried it doing this:-

Copy code
  1.    // Read in XML for the products and ranges
  2.     xml = loadXML("watch.xml");

  3.     // Get the number of product ranges
  4.     XML no_product_lines = xml.getChild("number_ranges");
  5.     // Get all children for watch 
  6.     XML[] children = xml.getChildren("products/watch");
  7.   
  8.     // Parse all watches from the XML file
  9.     for (int i = 0; i < children.length; i++) {
  10.       watch_range = children[i].getInt("range");
  11.     
  12.       // Only want information for one range of watch from selected range
  13.       if (watch_range == selected_range) {
  14.         // Parse required information from the XML file
  15.         watch_id = children[i].getInt("id");
  16.         watch_filename = children[i].getString("filename");
  17.         watch_description = children[i].getString("description");
  18.      
  19.         // How many watches in the range
  20.         watches_in_range_count++;
  21.       
  22.         // Do not process any more than 8 watches
  23.         if (watches_in_range_count <= 8)
  24.         {
  25.           // Display picture
  26.         
  27.           // Display description
  28.           if (watch_id != previous_id){
  29.             text("RANGE 1" + " WATCH ID = " + watch_id + " PREVIOUS ID " + previous_id + " FILENAME = " + watch_filename + " WATCH DESCRIPTION = " + watch_description,100,400 + watches_in_range_count * 30);
  30.           }
  31.           
  32.           // To ensure we don't repeat up to 8
  33.           previous_id = watch_id;
  34.         }
  35.       }
  36.     }
However, the code above counts the total amount of all watch childNodes no matter what the range attribute is.  

Here is the result, which is wrong - the code seems to restart the parsing from the beginning and repeat the parsing:-

RANGE 1 WATCH ID = 1 PREVIOUS ID = 0 FILENAME range1watch1.jpg WATCH DESCRIPTION = Description 1
RANGE 1 WATCH ID = 2 PREVIOUS ID = 1 FILENAME range1watch2.jpg WATCH DESCRIPTION = Description 2
RANGE 1 WATCH ID = 3 PREVIOUS ID = 2 FILENAME range1watch3.jpg WATCH DESCRIPTION = Description 3
RANGE 1 WATCH ID = 5 PREVIOUS ID = 3 FILENAME range1watch5.jpg WATCH DESCRIPTION = Description 5
RANGE 1 WATCH ID = 6 PREVIOUS ID = 5 FILENAME range1watch6.jpg WATCH DESCRIPTION = Description 6
RANGE 1 WATCH ID = 7 PREVIOUS ID = 6 FILENAME range1watch7.jpg WATCH DESCRIPTION = Description 7
RANGE 1 WATCH ID = 8 PREVIOUS ID = 7 FILENAME range1watch8.jpg WATCH DESCRIPTION = Description 8
RANGE 1 WATCH ID = 1 PREVIOUS ID = 8 FILENAME range1watch1.jpg WATCH DESCRIPTION = Description 1

The last entry is repeated, so it seems the parsing restarts.  Seems I am using the wrong approach.

How can I only parse the watch childNodes based on one range ID.  For example, if I want all watch childNodes for range attribute 2, I only want to parse 3 childNodes.

Please help if you can.  thanks :)

Replies(2)

I made a XML file from the one you provided, I added variable declarations to make your code fragment to work, and added some println to show what is going on.
And I cannot reproduce your problem:
1
RANGE 1 WATCH ID = 1 PREVIOUS ID -1 FILENAME = range1watch1.jpg WATCH DESCRIPTION = Description 1
1
RANGE 1 WATCH ID = 2 PREVIOUS ID 1 FILENAME = range1watch2.jpg WATCH DESCRIPTION = Description 2
1
RANGE 1 WATCH ID = 3 PREVIOUS ID 2 FILENAME = range1watch3.jpg WATCH DESCRIPTION = Description 3
1
RANGE 1 WATCH ID = 5 PREVIOUS ID 3 FILENAME = range1watch5.jpg WATCH DESCRIPTION = Description 5
1
RANGE 1 WATCH ID = 6 PREVIOUS ID 5 FILENAME = range1watch6.jpg WATCH DESCRIPTION = Description 6
1
RANGE 1 WATCH ID = 7 PREVIOUS ID 6 FILENAME = range1watch7.jpg WATCH DESCRIPTION = Description 7
1
RANGE 1 WATCH ID = 8 PREVIOUS ID 7 FILENAME = range1watch8.jpg WATCH DESCRIPTION = Description 8
2
2
2
Code:
Copy code
  1. size(1000, 800);
  2. background(255);
  3. fill(0);
  4. int selected_range = 1;
  5. int watches_in_range_count = 0;
  6. int previous_id = -1;
  7.     // Read in XML for the products and ranges
  8.     XML xml = loadXML("H:/Temp/watch.xml");
  9.     // Get the number of product ranges
  10.     XML no_product_lines = xml.getChild("number_ranges");
  11.     // Get all children for watch
  12.     XML[] children = xml.getChildren("products/watch");
  13.  
  14.     // Parse all watches from the XML file
  15.     for (int i = 0; i < children.length; i++) {
  16.       int watch_range = children[i].getInt("range");
  17.       println(watch_range);
  18.    
  19.       // Only want information for one range of watch from selected range
  20.       if (watch_range == selected_range) {
  21.         // Parse required information from the XML file
  22.         int watch_id = children[i].getInt("id");
  23.         String watch_filename = children[i].getString("filename");
  24.         String watch_description = children[i].getString("description");
  25.     
  26.         // How many watches in the range
  27.         watches_in_range_count++;
  28.      
  29.         // Do not process any more than 8 watches
  30.         if (watches_in_range_count <= 8)
  31.         {
  32.           // Display picture
  33.        
  34.           // Display description
  35.           if (watch_id != previous_id) {
  36.             String displayed = "RANGE 1" + " WATCH ID = " + watch_id + " PREVIOUS ID " + previous_id + " FILENAME = " + watch_filename + " WATCH DESCRIPTION = " + watch_description;
  37.             println(displayed);
  38.             text(displayed, 100, 400 + watches_in_range_count * 30);
  39.           }
  40.          
  41.           // To ensure we don't repeat up to 8
  42.           previous_id = watch_id;
  43.         }
  44.       }
  45.     }
  46. noLoop();
So perhaps your problem is in the code you haven't shown...
You're a star, thank you.  I had the code above in the DRAW procedure, and this is why it wasn't working!  

So, I moved it out and into the correct procedure and it works!

Thank you for helping me out! :)