Parsing XML
XML is used by a variety of applications. It's a really handy file format for structuring data that can easily be read by humans and machines. I've used the same data as in the CSV example but converted it to XML. It looks like the following screenshot:
How to do it...
The first thing we need to do is to declare an XML object. The loadXML()
function will be used to load the XML file into this object.
XML xml; void setup() { xml = loadXML( "processing-websites.xml" ); noLoop(); }
Inside the draw()
function, we'll loop through the XML document and use the getName()
, getInt()
, getString()
, and getContent()
functions, to get the data out of the structure.
void draw() { XML[] kids = xml.getChildren("website"); for ( int i = 0; i < kids.length; i++ ) { int id = kids[i].getInt("id"); String url = kids[i].getString("url"); String txt = kids[i].getContent(); println( i + ": " + id + " " + url + " " + txt ); } }
If you run the sketch, you should see the...