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.

18.8<br />

Using the <strong>Processing</strong> XML Library<br />

Data Input 349<br />

Th e functionality of the simpleML library, though easy <strong>to</strong> use, is quite limited. If you want <strong>to</strong> create your<br />

own XML documents, or parse multiple elements of a document with a cus<strong>to</strong>m algorithm, you are out of<br />

luck. For more sophisticated XML functionality, there are two options. Th e fi rst, more advanced option is<br />

proXML ( http://www.texone.org/proxml/ ) created by Christian Riekoff . While the learning curve is a bit<br />

steeper, you have direct access <strong>to</strong> the XML tree structure and can read and write XML documents.<br />

Th e second option, which we will explore in this chapter, is <strong>Processing</strong> ’s built-in XML library.<br />

import processing.xml.*;<br />

Once the library is imported, the fi rst step is <strong>to</strong> create an XMLElement object. Th is object will load the<br />

data from XML documents (s<strong>to</strong>red locally or on the web). Th e construc<strong>to</strong>r requires two arguments, “ this ”<br />

and the fi lename or URL path for the XML document.<br />

String url = " xmldocument.xml";<br />

XMLElement xml = new XMLElement(this,url);<br />

Unlike simpleML, this XML library pauses the sketch and waits for the document <strong>to</strong> load. For an<br />

asynchronous approach <strong>to</strong> XML parsing, you will need <strong>to</strong> use proXML.<br />

An XMLElement object represents one element of an XML tree. When a document is fi rst loaded, that<br />

element object is always the root element. simpleML did the work of traversing the tree and fi nding the<br />

right information for us. With the <strong>Processing</strong> XML library, we have <strong>to</strong> do this work ourselves. Although<br />

this is more complex, we have more control over how we search and what we search for.<br />

Referring back <strong>to</strong> Figure 18.13 , we can fi nd the temperature via the following path:<br />

1. Th e root element of the tree is “ RSS. ”<br />

2. “ RSS ” has a child element named “ Channel. ”<br />

3. Th e 13th child of “ Channel ” is “ item. ” (Th e diagram is simplifi ed <strong>to</strong> show only one child of channel.)<br />

4. Th e sixth child of “ item ” is “ yweather:condition. ”<br />

5. Th e temperature is s<strong>to</strong>red in “ yweather:condition ” as the attribute “ temp. ”<br />

Th e children of an element are accessed with an index (starting at zero, same as an array) passed in<strong>to</strong> the<br />

getChild( ) function. Th e content of an element is retrieved with getContent( ) and attributes are read as<br />

either numbers— getIntAttribute( ), getFloatAttribute( ) —or text— getStringAttribute( ) .<br />

// Accessing the first child element of the root element<br />

XMLElement channel = xml.getChild(0);<br />

Following steps 1 through 5 outlined above through the XML tree, we have:<br />

XMLElement xml = new XMLElement(this, url);<br />

XMLElement channel = xml.getChild(0);<br />

XMLElement item = channel.getChild(12);<br />

XMLElement condition = item.getChild(5);<br />

temp = condition.getIntAttribute( "temp");<br />

The 13th child of an element is index #12.

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

Saved successfully!

Ooh no, something went wrong!