|
Author |
Topic: XML, anyone? (Read 732 times) |
|
amoeba
|
XML, anyone?
« on: Apr 13th, 2004, 12:30pm » |
|
I know that several people out there have worked on XML parsers for Processing. Anything you'd like to share? Just hacking up a simple RSS parser (references here and here) for my own use at the moment, which I will post even though it'll be pretty dirty. Having worked with XML in Flash lately I think that it would be great for introducing students etc. to structured data as well as text data formats in general.
|
« Last Edit: Apr 13th, 2004, 12:43pm by amoeba » |
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
TomC
|
Re: XML, anyone?
« Reply #1 on: Apr 13th, 2004, 5:06pm » |
|
If you don't need namespaces or mixed content, then you might get some mileage out of this article http://www.devx.com/xml/Article/10114 It provides two classes which can be used as a fairly reliable DOM parser - good if you want access to the whole document in one go. The problem I find with XML parsers (I have also used MinML2 for SAX parsing, but it's messy) is that all the type conversion gets in the way. Would it be possible to extend Processing's casting/conversion functions int(), float() to work on strings so we don't have to use Integer.parseInt() and Float.parseFloat()? Has anyone started a Processing style, basically functional, XML parser library yet? How would it look? I'd be willing to convert the code from the article above to work like this... Code: void setup() { BElement root = parseXML("sampleRss091.xml"); // try "http://cyber.law.harvard.edu/blogs/gems/tech/sampleRss091.xml" // print it out again... printElement(root); } // recursively print the XML elements back to the console... void printElement(BElement element) { print("<" + element.name); for (int i = 0; i < element.attributes.length; i++) { print(" " + element.attributes[i].name + "=\"" + element.attributes[i].value + "\""); } println(">"); if (element.children.length > 0) { for (int i = 0; i < element.children.length; i++) { printElement(element.children[i]); } } else { println(element.content); } println("</" + element.name + ">"); } |
| Assuming I've not overlooked something major, what do people think?
|
« Last Edit: Apr 13th, 2004, 6:10pm by TomC » |
|
|
|
|
TomC
|
Re: XML, anyone?
« Reply #2 on: Apr 13th, 2004, 6:03pm » |
|
First attempt... excuse the mess it makes of the console, I just converted all the comments to println so I could see what it was doing update: Code was too long. Try here... http://www.tom-carden.co.uk/p5/XML.pde There's a parseXML method which takes a String (path to a file or URL), a BAttribute class which has a name and value (as Strings) and a BElement class (name as a String, content as a String, children as an array of BElements, attributes as an array of BAttributes). It uses a modified version of the DevX article's SimpleDOMParser to do the work, so it should be Java 1.1 compatible. Not sure about the collections I used though. NB:- It won't work with namespaces (so it's no good for RSS 2.0) or mixed content (e.g. <element>content<child>content</child></element>) (so it's no good for XHTML).
|
« Last Edit: Apr 13th, 2004, 6:15pm by TomC » |
|
|
|
|
amoeba
|
Re: XML, anyone?
« Reply #3 on: Apr 13th, 2004, 8:30pm » |
|
Ah, excellent. That's perfect for parsing RSS 0.91 content. Now I feel lazy for asking and not coding it myself, but hell... Thanks, tom!
|
« Last Edit: Apr 13th, 2004, 8:30pm by amoeba » |
|
marius watz // amoeba http://processing.unlekker.net/
|
|
|
|