06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

350 <strong>Learning</strong> <strong>Processing</strong><br />

Other useful functions that can call XMLElement objects are:<br />

• getChildCount( ) —returns the <strong>to</strong>tal number of children of an XMLElement.<br />

• getChildren( ) —returns all of the children as an array of XMLElements.<br />

In Example 18-3 , we used a series of comma separated values in a text fi le <strong>to</strong> s<strong>to</strong>re information related <strong>to</strong><br />

Bubble objects. An XML document can also be used in the same manner. Here is a possible solution <strong>to</strong><br />

Exercise 18-11, an XML tree of Bubble objects. (Note that this solution uses element attributes for red<br />

and green colors; this was not the format provided in Exercise 18-11 since we had not yet learned about<br />

attributes.)<br />

� ?xml version = " 1.0 " ? �<br />

� bubbles �<br />

� bubble �<br />

� diameter � 40 � /diameter �<br />

� color red = " 75 " green = " 255 " / �<br />

� /bubble �<br />

� bubble �<br />

� diameter � 20 � /diameter �<br />

� color red = " 255 " green = " 75" / �<br />

� /bubble �<br />

� bubble �<br />

� diameter � 80 � /diameter �<br />

� color red = " 100 " green = " 150 " / �<br />

� /bubble �<br />

� /bubbles �<br />

We can use getChildren( ) <strong>to</strong> receive the array of “ Bubble ” elements and make a Bubble object from each<br />

one. Here is the example (which uses the identical Bubble class from earlier). Th e new elements are in bold.<br />

Example 18-9: Using <strong>Processing</strong>’s XML library<br />

import processing.xml.*;<br />

// An array of Bubble objects<br />

Bubble[] bubbles;<br />

void setup() {<br />

size(200,200);<br />

smooth();<br />

// Load an XML document<br />

XMLElement xml = new XMLElement(this, " bubbles.xml " );<br />

// Get the <strong>to</strong>tal number of bubbles<br />

int <strong>to</strong>talBubbles = xml.getChildCount();<br />

// Make an array the same size<br />

bubbles = new Bubble[<strong>to</strong>talBubbles];<br />

// Get all the child elements<br />

XMLElement[] children = xml.getChildren();<br />

for (int i = 0; i < children.length; i + + ) {<br />

// The diameter is child 0<br />

The root element is “bubbles”, which has<br />

three children.<br />

Each child “bubble” has two children,<br />

“diameter” and “color.” The “color” element<br />

has two attributes, “red” and “green”.<br />

Getting the <strong>to</strong>tal number of Bubble objects<br />

with getChildCount().

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!