MooTools XML Parser?

275 views
Skip to first unread message

Chris

unread,
Jul 24, 2008, 10:21:35 AM7/24/08
to MooTools Users
Does anyone know of a good MooTools based XML Parser? I'm looking for
something that can take XML and turn it into JSON or Arrays or
something JavaScript can easily iterate through.

In the past I have used JKL which works pretty well, but if anyone
knows of something better I would love to know about it.

Bryan Swift

unread,
Jul 24, 2008, 10:59:10 AM7/24/08
to mootool...@googlegroups.com
I had written one for mootools a little while ago based on someone else's xml parser let me see if I can dig it up.

As I recall it performed well for relatively small xml files. I did a bunch of speed tests but I don't think I have results other than the anecdotal don't parse big files because it is slow.

Guillermo Rauch

unread,
Jul 24, 2008, 10:59:38 AM7/24/08
to mootool...@googlegroups.com
Take a look at how Request.HTML handles the XML response.
--
Guillermo Rauch
http://devthought.com

SilverTab

unread,
Jul 24, 2008, 11:48:22 AM7/24/08
to MooTools Users
Even without using any framework/library... the XMLHttpRequest object
automatically creates a "responseXML" which contains an "XmlDocument"
object on which you can use DOM methods... (after all, this what
XMLHttpRequest was originally intended to.. retrieve xml data)...

With Request.HTML, from the doc:
responseTree - (element) The node list of the remote response.


On Jul 24, 10:59 am, "Guillermo Rauch" <rau...@gmail.com> wrote:
> Take a look at how Request.HTML handles the XML response.
>

Guillermo Rauch

unread,
Jul 24, 2008, 11:56:21 AM7/24/08
to mootool...@googlegroups.com
On Thu, Jul 24, 2008 at 12:48 PM, SilverTab <php...@gmail.com> wrote:

Even without using any framework/library... the XMLHttpRequest object
automatically creates a "responseXML" which contains an "XmlDocument"
object on which you can use DOM methods... (after all, this what
XMLHttpRequest was originally intended to.. retrieve xml data)...
Which is not used in MT. Read Request.HTML code and you'll see that Microsoft.XMLDOM and DOMParser are used.
An alternative method here: http://www.quirksmode.org/dom/importxml.html, which uses document.implementation
Furthermore, it's possible that his xml is not coming from a XHR call.

Chris

unread,
Jul 24, 2008, 12:35:40 PM7/24/08
to MooTools Users
Thanks,

I havn't used Request.HTML before.

What exactly does the responseTree contain? an array of top level
nodes?


On Jul 24, 11:56 am, "Guillermo Rauch" <rau...@gmail.com> wrote:

SilverTab

unread,
Jul 24, 2008, 1:35:20 PM7/24/08
to MooTools Users
"Which is not used in MT. Read Request.HTML code and you'll see that
Microsoft.XMLDOM and DOMParser are used."

It appears to be the case indeed... Still, I just mentioned the fact
that the XMLHttpRequest object does offer a responseXML object that is
easy to work with... and which IS used by the Request class...

"Furthermore, it's possible that his xml is not coming from a XHR
call."

In this case, why not just use DOMParse or Microsoft.XMLDOM... both
can parse a string of text?


On Jul 24, 11:56 am, "Guillermo Rauch" <rau...@gmail.com> wrote:

Bryan Swift

unread,
Jul 24, 2008, 2:33:44 PM7/24/08
to mootool...@googlegroups.com
It was my understanding that he wanted to parse XML to a javascript object rather than a DOM tree. Perhaps I misunderstood?

Chris

unread,
Jul 24, 2008, 2:38:38 PM7/24/08
to MooTools Users
The DOM tree will work, to be honest I never even thought of that.

I'm just having trouble understanding what responseTree contains.

As far as I can tell it contains an array of top level nodes
(elements).

Is this correct, or am I way off in left field?


On Jul 24, 2:33 pm, "Bryan Swift" <bryan.j.sw...@gmail.com> wrote:
> It was my understanding that he wanted to parse XML to a javascript object
> rather than a DOM tree. Perhaps I misunderstood?
>

SilverTab

unread,
Jul 24, 2008, 3:24:05 PM7/24/08
to MooTools Users
The responseTree contains a NodeList (http://www.w3schools.com/DOM/
dom_nodelist.asp)

You can use the item(index) method on the nodelist to retrieve the
item located at "index"...

For example, using a simple xml file that looks like this:
<?xml version="1.0"?>
<MovieCatalog>
<movie>
<title>The Matrix</title>
<length>136</length>
<genre>Sci-Fi and Fantasy</genre>
<actors>
<actor>Keanu Reeves</actor>
<actor>Laurence Fishburne</actor>
<actor>Carrie Ann Moss</actor>
</actors>
<datereleased>1999</datereleased>
<director>Wachowski Brothers</director>
<format>DVD</format>
</movie>
</MovieCatalog>

The following would allow you to access "MovieCatalog"...

function onCompleteCallback(responseTree) {
responseTree.item(1);
}

Be careful though as this is not a good example: Firefox treats
whitespace and new lines as text nodes while IE doesn't... so you
should test for nodeType to make sure you get elements only...
(nodeType == 1)

SilverTab

unread,
Jul 24, 2008, 3:57:31 PM7/24/08
to MooTools Users
Just a quick add-on to my previous post, you could use the
responseElements to access the moviecatalog item (using my previous
example)

function onCompleteCallback(responseTree, responseElements) {
responseElements.filter('moviecatalog')[0]; // This will get you
the MovieCatalog element
responseElements.filter('movie'); // This would retrive an array
of all the "movie" items...

Iván N Paz

unread,
Aug 10, 2008, 10:38:46 PM8/10/08
to mootool...@googlegroups.com
Kinda "off the way".. but... why not use JSON instead??? Isnt it more
"portable"????
Reply all
Reply to author
Forward
0 new messages