Hi, I changed the XML parser who have been write by Greg (Gregory Hill)
and changed some things.
It did not work in IE and Safari and opera.
I fixed some bugs. Now it works fine in IE, Safari, Firefox and Opera.
/* --------------------------------------- */
XMLParser = Class.create();
Object.extend(XMLParser.prototype,
{
initialize: function (XMLFile, options)
{
this.xmlFile = XMLFile;
this.options = options;
this.element = false;
this.XMLParsed = false;
this.getXml();
},
XMLHash: function ()
{
return this.XMLParsed;
},
asHash: function ()
{
if (! this._xmlHash) {
this._xmlHash = this._nodeToHash(this.element);
}
return this._xmlHash;
},
getXml: function ()
{
if(this.options.onLoading) {
this.options.onLoading();
}
new Ajax.Request(this.xmlFile, {method:''get'',
onComplete: this.parseXML.bind(this)});
},
parseXML: function (req)
{
this.element = req.responseXML;
this.XMLParsed = this.asHash();
if(this.options.onComplete) {
this.options.onComplete(this.XMLParsed);
}
},
_nodeToHash: function (node)
{
Element.cleanWhitespace(node);
//
// remove comments, firefox fix
node = this._removeComments(node);
if ((node.attributes && node.attributes.length > 0) ||
(node.hasChildNodes() && (node.childNodes[0].nodeType == 1 ||
node.childNodes[0].nodeType == 7))) {
var localHash = {};
if (node.attributes && node.attributes.length >= 1) {
$A(node.attributes).each(function (attr) {
localHash[attr.nodeName] = [attr.nodeValue];
});
}
if (node.hasChildNodes() && (node.childNodes[0].nodeType ==
1 || node.childNodes[0].nodeType == 7)) {
$A(node.childNodes).each(function (node) {
this._subNodeToHash(localHash, node);
}.bindAsEventListener(this));
}
else if (node.hasChildNodes()) {
localHash[''text''] = [this._nodeAsText(node)];
}
$H(localHash).each( function (pair) {
if (pair[1].length == 1 && typeof pair[1][0] ==
''string'') {
localHash[pair[0]] = pair[1][0];
}
});
return localHash;
} else {
return this._nodeAsText(node);
}
},
_removeComments: function(node)
{
for(var i=0; i < node.childNodes.length; i++) {
var nodeTmp = node.childNodes[i];
if(nodeTmp.nodeType == 8) {
Element.remove(nodeTmp);
}
}
return node;
},
_subNodeToHash: function (hash, node)
{
var key = node.tagName;
if (hash[key]) {
hash[key].push(this._nodeToHash(node));
} else {
hash[key] = [ this._nodeToHash(node) ];
}
},
_nodeAsText: function (node)
{
return node.textContent || node.innerText || node.text ||
node.childNodes[0].nodeValue || '''';
}
}
);
/* --------------------------------------- */
/* How To use it */
/* --------------------------------------- */
<*cript>
function showBlog(xml)
{
$(''outputField'').innerHTML = '''';
var total =
xml.rss[0][''channel''][0][''item''].length;
for(var i=0; i < total; i++) {
$(''outputField'').innerHTML += ''<a
href="''+xml.rss[0][''channel''][0][''item''][i][''link'']+''"
target="_blank"><b>''+xml.rss[0][''channel''][0][''item''][i][''title'']+''</b></a><br
/>'';
$(''outputField'').innerHTML +=
xml.rss[0][''channel''][0][''item''][i][''description'']+''<hr>'';
}
}
</*cript>
<input type="button" name="b_rss" value="RSS 2"
onClick="new
XMLParser(''./myrss2.xml''),{onComplete:showBlog});" />
/* --------------------------------------- */
Enjoy
Claudio Gamboa
> Hi, I changed the XML parser who have been write by Greg (GregoryHill)> and changed some things. > > It did not work in IE and Safari and opera.Um... I tested mine in IE and Opera without any problems. Or are you saying that the stuff you added did not work in those browsers? Greg
Hi well, I tested it with a RSS and it didn''t work in IE. It returns a nodeType = 7 in the first node. And if the RSS has a comment in it doesn''t work too, because the nodeType = 8 BTW, for your code that I have see you are a good JS programmer. On Thu, 2006-03-23 at 10:40 -0700, Gregory Hill wrote:> > Hi, I changed the XML parser who have been write by Greg (Gregory > Hill) > > and changed some things. > > > > It did not work in IE and Safari and opera. > > Um... I tested mine in IE and Opera without any problems. Or are you > saying that the stuff you added did not work in those browsers? > > Greg > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thanks guys for taking a stab at this. One quick question - all these user contributed code - do you guys put it on the wiki? Or the only way to get to this is by searching through the 1000s of threads? I think we all should try to put as much focus on documentation as we are putting on coding. Thank you, Mandy. p.s. If there are any document wishes, I am willing to take the steps.
Heya, On the documentation: First of all, pages marked with the "stub" tag are needed, as are the "wanted pages" (pages referenced, but not created). See: http://wiki.script.aculo.us/scriptaculous/tags/stub Plus, feel free to add documentation, where you feel it''s needed. The FAQ could also need some some love. -Thomas Am 26.03.2006 um 14:52 schrieb Maninder, Singh:> Thanks guys for taking a stab at this. > > One quick question - all these user contributed code - do you guys > put it on the wiki? > > Or the only way to get to this is by searching through the 1000s of > threads? > > I think we all should try to put as much focus on documentation as > we are putting on coding. > > Thank you, > Mandy. > > p.s. If there are any document wishes, I am willing to take the steps. > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs