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:-
- <product_lines>
- <number_ranges id="2" />
- <products>
- <watch id="1" range="1" filename="range1watch1.jpg" description="Description 1" />
- <watch id="2" range="1" filename="range1watch2.jpg" description="Description 2" />
- <watch id="3" range="1" filename="range1watch3.jpg" description="Description 3" />
- <watch id="5" range="1" filename="range1watch5.jpg" description="Description 5" />
- <watch id="6" range="1" filename="range1watch6.jpg" description="Description 6" />
- <watch id="7" range="1" filename="range1watch7.jpg" description="Description 7" />
- <watch id="8" range="1" filename="range1watch8.jpg" description="Description 8" />
- <watch id="9" range="2" filename="range2watch1.jpg" description="Description 1" />
- <watch id="10" range="2" filename="range2watch2.jpg" description="Description 2" />
- <watch id="11" range="2" filename="range2watch3.jpg" description="Description 3" />
- </products>
- </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:-
- // Read in XML for the products and ranges
- xml = loadXML("watch.xml");
- // Get the number of product ranges
- XML no_product_lines = xml.getChild("number_ranges");
- // Get all children for watch
- XML[] children = xml.getChildren("products/watch");
- // Parse all watches from the XML file
- for (int i = 0; i < children.length; i++) {
- watch_range = children[i].getInt("range");
- // Only want information for one range of watch from selected range
- if (watch_range == selected_range) {
- // Parse required information from the XML file
- watch_id = children[i].getInt("id");
- watch_filename = children[i].getString("filename");
- watch_description = children[i].getString("description");
- // How many watches in the range
- watches_in_range_count++;
- // Do not process any more than 8 watches
- if (watches_in_range_count <= 8)
- {
- // Display picture
- // Display description
- if (watch_id != previous_id){
- 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);
- }
- // To ensure we don't repeat up to 8
- previous_id = watch_id;
- }
- }
- }
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 :)
1