Re: [xmljs-users] Comparision values returned by getAttribute()
Brought to you by:
djoham,
witchhunter
From: David J. <dj...@ya...> - 2005-03-24 20:50:06
|
Wow, you learn something new every day... The reason you're seeing this is that the attribute value variables are created like this: atributeValue = new String(valueFromXML); What I didn't know until just now is that if you have the following construct in JavaScript, you'll be doing an object comparison rather than a string comparison: var x = new String("FOO"); var y = new String("FOO"); //object comparison alert(x==y); If you do an alert(typeof x), you'll get a return value of "object" rather than "string" This is why your comparison is failing, but I without more research I can't tell you why creating a string with new String() doesn't actually create a "string" object. Thankfully, the fix is easy :) On or about line 2859 in xmlw3cdom.js, you'll see a line of code that says "return ret". The fix is to change that line to "return ret.toString()" I've included the full function below (with the fix) just to provide more context: DOMElement.prototype.getAttribute = function DOMElement_getAttribute(name) { var ret = ""; // if attribute exists, use it var attr = this.attributes.getNamedItem(name); if (attr) { ret = attr.value; } return ret.toString(); // if Attribute exists, return its value, otherwise, return "" }; Hopefully that will get you going. By the way, thanks for the *great* testcase. Good luck! David --- san...@ya... wrote: > It seems like comparing the string value returned by > getAttribute() always results in failure even though > they have the same value: > > <HTML> > <head> > <title>Test</title> > </head> > <script src="../jsXMLParser/xmlw3cdom.js"></script> > <script src="../jsXMLParser/xmlsax.js"></script> > <script> > function foo(){ > var strXML = "<root id=\"root\"><child > id=\"child1\"/><child id=\"child1\"/></root>"; > > //instantiate the W3C DOM Parser > var parser = new DOMImplementation(); > > //load the XML into the parser and get the > DOMDocument > var domDoc = parser.loadXML(strXML); > > var children = > domDoc.getDocumentElement().getChildNodes(); > > > var str1 = children.item(0).getAttribute("id"); > var str2 = children.item(1).getAttribute("id") > > alert ("str1 = " + str1 + ", str2 = " + str2); > > alert(str1 == str2); //always false <======== > } > </script> > <BODY onload="foo()"> > </BODY> > </HTML> > > Why is this? Is there a way around this? > > thanks. > Sanjeev > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > xmljs-users mailing list > xml...@li... > https://lists.sourceforge.net/lists/listinfo/xmljs-users > |