[xmljs-users] Re: xmljs-users digest, Vol 1 #63 - 2 msgs
Brought to you by:
djoham,
witchhunter
From: James S. E. <jam...@co...> - 2005-03-25 21:42:01
|
Sanjeev, This may be superfluous since David has provided a more detailed explanation but, when working with DOM attribute nodes, never assume the value will be a native string in the programming language you're using. DOM objects are built on DOM-specific primitive types - for portablilty among languages, I guess. Regards, James S. Elkins At 10:25 PM 3/24/2005, you wrote: >Send xmljs-users mailing list submissions to > xml...@li... > >To subscribe or unsubscribe via the World Wide Web, visit > https://lists.sourceforge.net/lists/listinfo/xmljs-users >or, via email, send a message with subject or body 'help' to > xml...@li... > >You can reach the person managing the list at > xml...@li... > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of xmljs-users digest..." > > >Today's Topics: > > 1. Comparision values returned by getAttribute() (san...@ya...) > 2. Re: Comparision values returned by getAttribute() (David Joham) > >--__--__-- > >Message: 1 >Date: Thu, 24 Mar 2005 12:12:27 -0800 (PST) >From: <san...@ya...> >To: xml...@li... >Subject: [xmljs-users] Comparision values returned by getAttribute() > >It seems like comparing the string value returned by >getAttribute() always results in failure even though >they have the same value: > > >Why is this? Is there a way around this? > >thanks. >Sanjeev > > >--__--__-- > >Message: 2 >Date: Thu, 24 Mar 2005 12:49:57 -0800 (PST) >From: David Joham <dj...@ya...> >Subject: Re: [xmljs-users] Comparision values returned by getAttribute() >To: san...@ya..., xml...@li... > > >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 > > > > > >--__--__-- > >_______________________________________________ >xmljs-users mailing list >xml...@li... >https://lists.sourceforge.net/lists/listinfo/xmljs-users > > >End of xmljs-users Digest |