2

I am trying to use data extracted from a XML file by getElementByTagName and it returns HTML Collection Object but I need this data for a sending a REST request so I need to get the HTML Collection Object to be converted into a string. How can it be done?

Here's more information:

com_zimbra_om.prototype._responseHandler=
        function(response){
                try{
                    sid = response.xml.getElementsByTagName("session_id");
                    this.login_user();
                    }catch(e){
                            this._showErrorMsg(e);
                            }

Using this function I am trying to get the session_id from a REST response. Here sid (global variable) is the HTML Collection Object. Now when I try to use this in the next function:

com_zimbra_om.prototype.login_user = function(){
var url = selected_server + 'services/UserService/loginUser?SID=' +
                                    sid + '&username='+
                                    selected_username +
                                    '&userpass=' + 
                                    selected_password;
                var request_url = ZmZimletBase.PROXY + AjxStringUtil.urlComponentEncode(url);

So here I am using sid which I need as a string.

So how should I convert HTML Collection Object into string??

Thanks

5
  • 3
    And how exactly should the string representation of such a collection look like? Please be more precise and provide some example code with input and output. Jun 20, 2012 at 18:27
  • I doubt you get a HTMLCollection from a XML document...
    – Bergi
    Jun 20, 2012 at 18:31
  • @Bergi document.getElementsByTagName("div") + "" in firefox gives that, see bugzilla.mozilla.org/show_bug.cgi?id=14869
    – Esailija
    Jun 20, 2012 at 18:34
  • You can also run var a = DOMParser(); var xml = "<node></node>"; xml = a.parseFromString(xml, "application/xml" ); xml.getElementsByTagName("node") + ""; //"[object HTMLCollection]" To see it's not any different with XML document.
    – Esailija
    Jun 20, 2012 at 18:38
  • @Esailija: OK, thanks for the bug link. I expected NodeLists, too (as it happens to work in Opera for example :-)
    – Bergi
    Jun 20, 2012 at 18:46

1 Answer 1

9

With this information I can only go with

var objectHTMLCollection = document.getElementsByTagName("div"),
    string = [].map.call( objectHTMLCollection, function(node){
        return node.textContent || node.innerText || "";
    }).join("");
1
  • can we store a full html code in a string variable for eg var a =" <html><head></head><body></body></html>" or a raw html code in a string Jun 29, 2018 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.