3 ** pz2.js - pazpar2's javascript client library.
6 //since explorer is flawed
8 window.Node = new Object();
10 Node.ATTRIBUTE_NODE = 2;
12 Node.CDATA_SECTION_NODE = 4;
13 Node.ENTITY_REFERENCE_NODE = 5;
15 Node.PROCESSING_INSTRUCTION_NODE = 7;
16 Node.COMMENT_NODE = 8;
17 Node.DOCUMENT_NODE = 9;
18 Node.DOCUMENT_TYPE_NODE = 10;
19 Node.DOCUMENT_FRAGMENT_NODE = 11;
20 Node.NOTATION_NODE = 12;
23 // prevent execution of more than once
24 if(typeof window.pz2 == "undefined") {
25 window.undefined = window.undefined;
27 var pz2 = function ( paramArray )
30 // at least one callback required
32 throw new Error("Pz2.js: Array with parameters has to be suplied.");
34 //supported pazpar2's protocol version
35 this.suppProtoVer = '1';
36 if (typeof paramArray.pazpar2path != "undefined")
37 this.pz2String = paramArray.pazpar2path;
39 this.pz2String = "/pazpar2/search.pz2";
40 this.useSessions = true;
42 this.stylesheet = paramArray.detailstylesheet || null;
43 //load stylesheet if required in async mode
44 if( this.stylesheet ) {
46 var request = new pzHttpRequest( this.stylesheet );
47 request.get( {}, function ( doc ) { context.xslDoc = doc; } );
50 this.errorHandler = paramArray.errorhandler || null;
51 this.showResponseType = paramArray.showResponseType || "xml";
54 this.initCallback = paramArray.oninit || null;
55 this.statCallback = paramArray.onstat || null;
56 this.showCallback = paramArray.onshow || null;
57 this.termlistCallback = paramArray.onterm || null;
58 this.recordCallback = paramArray.onrecord || null;
59 this.bytargetCallback = paramArray.onbytarget || null;
60 this.resetCallback = paramArray.onreset || null;
63 this.termKeys = paramArray.termlist || "subject";
65 // some configurational stuff
66 this.keepAlive = 50000;
68 if ( paramArray.keepAlive < this.keepAlive )
69 this.keepAlive = paramArray.keepAlive;
71 this.sessionID = null;
72 this.serviceId = paramArray.serviceId || null;
73 this.initStatusOK = false;
74 this.pingStatusOK = false;
75 this.searchStatusOK = false;
78 this.currentSort = "relevance";
81 this.currentStart = 0;
82 // currentNum can be overwritten in show
85 // last full record retrieved
86 this.currRecID = null;
89 this.currQuery = null;
91 //current raw record offset
92 this.currRecOffset = null;
95 this.pingTimer = null;
96 this.statTime = paramArray.stattime || 1000;
97 this.statTimer = null;
98 this.termTime = paramArray.termtime || 1000;
99 this.termTimer = null;
100 this.showTime = paramArray.showtime || 1000;
101 this.showTimer = null;
102 this.showFastCount = 4;
103 this.bytargetTime = paramArray.bytargettime || 1000;
104 this.bytargetTimer = null;
105 this.recordTime = paramArray.recordtime || 500;
106 this.recordTimer = null;
108 // counters for each command and applied delay
109 this.dumpFactor = 500;
110 this.showCounter = 0;
111 this.termCounter = 0;
112 this.statCounter = 0;
113 this.bytargetCounter = 0;
114 this.recordCounter = 0;
116 // active clients, updated by stat and show
117 // might be an issue since bytarget will poll accordingly
118 this.activeClients = 1;
120 // if in proxy mode no need to init
121 if (paramArray.usesessions != undefined) {
122 this.useSessions = paramArray.usesessions;
123 this.initStatusOK = true;
125 // else, auto init session or wait for a user init?
126 if (this.useSessions && paramArray.autoInit !== false) {
127 this.init(this.sessionId, this.serviceId);
133 //error handler for async error throws
134 throwError: function (errMsg, errCode)
136 var err = new Error(errMsg);
137 if (errCode) err.code = errCode;
139 if (this.errorHandler) {
140 this.errorHandler(err);
147 // stop activity by clearing tiemouts
150 clearTimeout(this.statTimer);
151 clearTimeout(this.showTimer);
152 clearTimeout(this.termTimer);
153 clearTimeout(this.bytargetTimer);
156 // reset status variables
159 if ( this.useSessions ) {
160 this.sessionID = null;
161 this.initStatusOK = false;
162 this.pingStatusOK = false;
163 clearTimeout(this.pingTimer);
165 this.searchStatusOK = false;
168 if ( this.resetCallback )
169 this.resetCallback();
172 init: function (sessionId, serviceId)
176 // session id as a param
177 if (sessionId && this.useSessions ) {
178 this.initStatusOK = true;
179 this.sessionID = sessionId;
181 // old school direct pazpar2 init
182 } else if (this.useSessions) {
184 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
185 var opts = {'command' : 'init'};
186 if (serviceId) opts.service = serviceId;
190 if ( data.getElementsByTagName("status")[0]
191 .childNodes[0].nodeValue == "OK" ) {
192 if ( data.getElementsByTagName("protocol")[0]
193 .childNodes[0].nodeValue
194 != context.suppProtoVer )
196 "Server's protocol not supported by the client"
198 context.initStatusOK = true;
200 data.getElementsByTagName("session")[0]
201 .childNodes[0].nodeValue;
209 if ( context.initCallback )
210 context.initCallback();
213 context.throwError('Init failed. Malformed WS resonse.',
217 // when through proxy no need to init
219 this.initStatusOK = true;
222 // no need to ping explicitly
225 // pinging only makes sense when using pazpar2 directly
226 if( !this.initStatusOK || !this.useSessions )
228 'Pz2.js: Ping not allowed (proxy mode) or session not initialized.'
232 clearTimeout(context.pingTimer);
234 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
236 { "command": "ping", "session": this.sessionID, "windowid" : window.name },
238 if ( data.getElementsByTagName("status")[0]
239 .childNodes[0].nodeValue == "OK" ) {
240 context.pingStatusOK = true;
250 context.throwError('Ping failed. Malformed WS resonse.',
255 search: function (query, num, sort, filter, showfrom, addParamsArr)
257 clearTimeout(this.statTimer);
258 clearTimeout(this.showTimer);
259 clearTimeout(this.termTimer);
260 clearTimeout(this.bytargetTimer);
262 this.showCounter = 0;
263 this.termCounter = 0;
264 this.bytargetCounter = 0;
265 this.statCounter = 0;
266 this.activeClients = 1;
269 if( !this.initStatusOK )
270 throw new Error('Pz2.js: session not initialized.');
272 if( query !== undefined )
273 this.currQuery = query;
275 throw new Error("Pz2.js: no query supplied to the search command.");
277 if ( showfrom !== undefined )
278 var start = showfrom;
284 "query": this.currQuery,
285 "session": this.sessionID,
286 "windowid" : window.name
289 if (filter !== undefined)
290 searchParams["filter"] = filter;
292 // copy additional parmeters, do not overwrite
293 if (addParamsArr != undefined) {
294 for (var prop in addParamsArr) {
295 if (!searchParams.hasOwnProperty(prop))
296 searchParams[prop] = addParamsArr[prop];
301 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
305 if ( data.getElementsByTagName("status")[0]
306 .childNodes[0].nodeValue == "OK" ) {
307 context.searchStatusOK = true;
309 context.show(start, num, sort);
310 if (context.statCallback)
312 if (context.termlistCallback)
314 if (context.bytargetCallback)
318 context.throwError('Search failed. Malformed WS resonse.',
325 if( !this.initStatusOK )
326 throw new Error('Pz2.js: session not initialized.');
328 // if called explicitly takes precedence
329 clearTimeout(this.statTimer);
332 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
334 { "command": "stat", "session": this.sessionID, "windowid" : window.name },
336 if ( data.getElementsByTagName("stat") ) {
338 Number( data.getElementsByTagName("activeclients")[0]
339 .childNodes[0].nodeValue );
340 context.activeClients = activeClients;
342 var stat = Element_parseChildNodes(data.documentElement);
344 context.statCounter++;
345 var delay = context.statTime
346 + context.statCounter * context.dumpFactor;
348 if ( activeClients > 0 )
356 context.statCallback(stat);
359 context.throwError('Stat failed. Malformed WS resonse.',
364 show: function(start, num, sort, query_state)
366 if( !this.searchStatusOK && this.useSessions )
368 'Pz2.js: show command has to be preceded with a search command.'
371 // if called explicitly takes precedence
372 clearTimeout(this.showTimer);
374 if( sort !== undefined )
375 this.currentSort = sort;
376 if( start !== undefined )
377 this.currentStart = Number( start );
378 if( num !== undefined )
379 this.currentNum = Number( num );
382 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
383 var requestParameters =
386 "session": this.sessionID,
387 "start": this.currentStart,
388 "num": this.currentNum,
389 "sort": this.currentSort,
391 "type": this.showResponseType,
392 "windowid" : window.name
395 requestParameters["query-state"] = query_state;
398 function(data, type) {
400 var activeClients = 0;
401 if (type === "json") {
403 activeClients = Number(data.activeclients[0]);
404 show.activeclients = activeClients;
405 show.merged = Number(data.merged[0]);
406 show.total = Number(data.total[0]);
407 show.start = Number(data.start[0]);
408 show.num = Number(data.num[0]);
409 show.hits = data.hit;
410 } else if (data.getElementsByTagName("status")[0]
411 .childNodes[0].nodeValue == "OK") {
412 // first parse the status data send along with records
413 // this is strictly bound to the format
415 Number(data.getElementsByTagName("activeclients")[0]
416 .childNodes[0].nodeValue);
418 "activeclients": activeClients,
420 Number( data.getElementsByTagName("merged")[0]
421 .childNodes[0].nodeValue ),
423 Number( data.getElementsByTagName("total")[0]
424 .childNodes[0].nodeValue ),
426 Number( data.getElementsByTagName("start")[0]
427 .childNodes[0].nodeValue ),
429 Number( data.getElementsByTagName("num")[0]
430 .childNodes[0].nodeValue ),
433 // parse all the first-level nodes for all <hit> tags
434 var hits = data.getElementsByTagName("hit");
435 for (i = 0; i < hits.length; i++)
436 show.hits[i] = Element_parseChildNodes(hits[i]);
438 context.throwError('Show failed. Malformed WS resonse.',
441 context.activeClients = activeClients;
442 context.showCounter++;
443 var delay = context.showTime;
444 if (context.showCounter > context.showFastCount)
445 delay += context.showCounter * context.dumpFactor;
446 if ( activeClients > 0 )
447 context.showTimer = setTimeout(
452 context.showCallback(show);
456 record: function(id, offset, syntax, handler)
458 // we may call record with no previous search if in proxy mode
459 if(!this.searchStatusOK && this.useSessions)
461 'Pz2.js: record command has to be preceded with a search command.'
464 if( id !== undefined )
469 "session": this.sessionID,
470 "id": this.currRecID,
471 "windowid" : window.name
474 this.currRecOffset = null;
475 if (offset != undefined) {
476 recordParams["offset"] = offset;
477 this.currRecOffset = offset;
480 if (syntax != undefined)
481 recordParams['syntax'] = syntax;
483 //overwrite default callback id needed
484 var callback = this.recordCallback;
485 var args = undefined;
486 if (handler != undefined) {
487 callback = handler['callback'];
488 args = handler['args'];
492 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
500 if (context.currRecOffset !== null) {
501 record = new Array();
502 record['xmlDoc'] = data;
503 record['offset'] = context.currRecOffset;
504 callback(record, args);
506 } else if ( recordNode =
507 data.getElementsByTagName("record")[0] ) {
508 // if stylesheet was fetched do not parse the response
509 if ( context.xslDoc ) {
510 record = new Array();
511 record['xmlDoc'] = data;
512 record['xslDoc'] = context.xslDoc;
514 recordNode.getElementsByTagName("recid")[0]
515 .firstChild.nodeValue;
518 record = Element_parseChildNodes(recordNode);
521 Number( data.getElementsByTagName("activeclients")[0]
522 .childNodes[0].nodeValue );
523 context.activeClients = activeClients;
524 context.recordCounter++;
525 var delay = context.recordTime + context.recordCounter * context.dumpFactor;
526 if ( activeClients > 0 )
527 context.recordTimer =
530 context.record(id, offset, syntax, handler);
534 callback(record, args);
537 context.throwError('Record failed. Malformed WS resonse.',
545 if( !this.searchStatusOK && this.useSessions )
547 'Pz2.js: termlist command has to be preceded with a search command.'
550 // if called explicitly takes precedence
551 clearTimeout(this.termTimer);
554 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
557 "command": "termlist",
558 "session": this.sessionID,
559 "name": this.termKeys,
560 "windowid" : window.name
563 if ( data.getElementsByTagName("termlist") ) {
565 Number( data.getElementsByTagName("activeclients")[0]
566 .childNodes[0].nodeValue );
567 context.activeClients = activeClients;
568 var termList = { "activeclients": activeClients };
569 var termLists = data.getElementsByTagName("list");
571 for (i = 0; i < termLists.length; i++) {
572 var listName = termLists[i].getAttribute('name');
573 termList[listName] = new Array();
574 var terms = termLists[i].getElementsByTagName('term');
575 //for each term in the list
576 for (j = 0; j < terms.length; j++) {
579 (terms[j].getElementsByTagName("name")[0]
581 ? terms[j].getElementsByTagName("name")[0]
582 .childNodes[0].nodeValue
586 .getElementsByTagName("frequency")[0]
587 .childNodes[0].nodeValue || 'ERROR'
591 terms[j].getElementsByTagName("id");
592 if(terms[j].getElementsByTagName("id").length)
594 termIdNode[0].childNodes[0].nodeValue;
595 termList[listName][j] = term;
599 context.termCounter++;
600 var delay = context.termTime
601 + context.termCounter * context.dumpFactor;
602 if ( activeClients > 0 )
611 context.termlistCallback(termList);
614 context.throwError('Termlist failed. Malformed WS resonse.',
622 if( !this.initStatusOK && this.useSessions )
624 'Pz2.js: bytarget command has to be preceded with a search command.'
627 // no need to continue
628 if( !this.searchStatusOK )
631 // if called explicitly takes precedence
632 clearTimeout(this.bytargetTimer);
635 var request = new pzHttpRequest(this.pz2String, this.errorHandler);
638 "command": "bytarget",
639 "session": this.sessionID,
641 "windowid" : window.name
644 if ( data.getElementsByTagName("status")[0]
645 .childNodes[0].nodeValue == "OK" ) {
646 var targetNodes = data.getElementsByTagName("target");
647 var bytarget = new Array();
648 for ( i = 0; i < targetNodes.length; i++) {
649 bytarget[i] = new Array();
650 for( j = 0; j < targetNodes[i].childNodes.length; j++ ) {
651 if ( targetNodes[i].childNodes[j].nodeType
652 == Node.ELEMENT_NODE ) {
654 targetNodes[i].childNodes[j].nodeName;
655 if (targetNodes[i].childNodes[j].firstChild != null)
657 var nodeText = targetNodes[i].childNodes[j]
658 .firstChild.nodeValue;
659 bytarget[i][nodeName] = nodeText;
662 bytarget[i][nodeName] = "";
668 if (bytarget[i]["state"]=="Client_Disconnected") {
669 bytarget[i]["hits"] = "Error";
670 } else if (bytarget[i]["state"]=="Client_Error") {
671 bytarget[i]["hits"] = "Error";
672 } else if (bytarget[i]["state"]=="Client_Working") {
673 bytarget[i]["hits"] = "...";
675 if (bytarget[i].diagnostic == "1") {
676 bytarget[i].diagnostic = "Permanent system error";
677 } else if (bytarget[i].diagnostic == "2") {
678 bytarget[i].diagnostic = "Temporary system error";
680 var targetsSuggestions = targetNodes[i].getElementsByTagName("suggestions");
681 if (targetsSuggestions != undefined && targetsSuggestions.length>0) {
682 var suggestions = targetsSuggestions[0];
683 bytarget[i]["suggestions"] = Element_parseChildNodes(suggestions);
687 context.bytargetCounter++;
688 var delay = context.bytargetTime
689 + context.bytargetCounter * context.dumpFactor;
690 if ( context.activeClients > 0 )
691 context.bytargetTimer =
699 context.bytargetCallback(bytarget);
702 context.throwError('Bytarget failed. Malformed WS resonse.',
708 // just for testing, probably shouldn't be here
709 showNext: function(page)
711 var step = page || 1;
712 this.show( ( step * this.currentNum ) + this.currentStart );
715 showPrev: function(page)
717 if (this.currentStart == 0 )
719 var step = page || 1;
720 var newStart = this.currentStart - (step * this.currentNum );
721 this.show( newStart > 0 ? newStart : 0 );
724 showPage: function(pageNum)
726 //var page = pageNum || 1;
727 this.show(pageNum * this.currentNum);
732 ********************************************************************************
733 ** AJAX HELPER CLASS ***********************************************************
734 ********************************************************************************
736 var pzHttpRequest = function ( url, errorHandler ) {
737 this.maxUrlLength = 2048;
740 this.errorHandler = errorHandler || null;
742 this.requestHeaders = {};
744 if ( window.XMLHttpRequest ) {
745 this.request = new XMLHttpRequest();
746 } else if ( window.ActiveXObject ) {
748 this.request = new ActiveXObject( 'Msxml2.XMLHTTP' );
750 this.request = new ActiveXObject( 'Microsoft.XMLHTTP' );
756 pzHttpRequest.prototype =
758 safeGet: function ( params, callback )
760 var encodedParams = this.encodeParams(params);
761 var url = this._urlAppendParams(encodedParams);
762 if (url.length >= this.maxUrlLength) {
763 this.requestHeaders["Content-Type"]
764 = "application/x-www-form-urlencoded";
765 this._send( 'POST', this.url, encodedParams, callback );
767 this._send( 'GET', url, '', callback );
771 get: function ( params, callback )
773 this._send( 'GET', this._urlAppendParams(this.encodeParams(params)),
777 post: function ( params, data, callback )
779 this._send( 'POST', this._urlAppendParams(this.encodeParams(params)),
786 this.request.open( 'GET', this.url, this.async );
787 this.request.send('');
788 if ( this.request.status == 200 )
789 return this.request.responseXML;
792 encodeParams: function (params)
796 for (var key in params) {
797 if (params[key] != null) {
798 encoded += sep + key + '=' + encodeURIComponent(params[key]);
805 _send: function ( type, url, data, callback)
808 this.callback = callback;
810 this.request.open( type, url, this.async );
811 for (var key in this.requestHeaders)
812 this.request.setRequestHeader(key, this.requestHeaders[key]);
813 this.request.onreadystatechange = function () {
814 context._handleResponse(url); /// url used ONLY for error reporting
816 this.request.send(data);
819 _urlAppendParams: function (encodedParams)
822 return this.url + "?" + encodedParams;
827 _handleResponse: function (savedUrlForErrorReporting)
829 if ( this.request.readyState == 4 ) {
830 // pick up appplication errors first
832 if (this.request.responseXML &&
833 (errNode = this.request.responseXML.documentElement)
834 && errNode.nodeName == 'error') {
835 var errMsg = errNode.getAttribute("msg");
836 var errCode = errNode.getAttribute("code");
838 if (errNode.childNodes.length)
839 errAddInfo = ': ' + errNode.childNodes[0].nodeValue;
841 var err = new Error(errMsg + errAddInfo);
844 if (this.errorHandler) {
845 this.errorHandler(err);
850 } else if (this.request.status == 200 &&
851 this.request.responseXML == null) {
852 if (this.request.responseText != null) {
856 var text = this.request.responseText;
857 if (typeof window.JSON == "undefined")
858 json = eval("(" + text + ")");
861 json = JSON.parse(text);
864 // Safari: eval will fail as well. Considering trying JSON2 (non-native implementation) instead
865 /* DEBUG only works in mk2-mobile
866 if (document.getElementById("log"))
867 document.getElementById("log").innerHTML = "" + e + " " + length + ": " + text;
870 json = eval("(" + text + ")");
873 /* DEBUG only works in mk2-mobile
874 if (document.getElementById("log"))
875 document.getElementById("log").innerHTML = "" + e + " " + length + ": " + text;
880 this.callback(json, "json");
882 var err = new Error("XML response is empty but no error " +
883 "for " + savedUrlForErrorReporting);
885 if (this.errorHandler) {
886 this.errorHandler(err);
891 } else if (this.request.status == 200) {
892 this.callback(this.request.responseXML);
894 var err = new Error("HTTP response not OK: "
895 + this.request.status + " - "
896 + this.request.statusText );
897 err.code = '00' + this.request.status;
898 if (this.errorHandler) {
899 this.errorHandler(err);
910 ********************************************************************************
911 ** XML HELPER FUNCTIONS ********************************************************
912 ********************************************************************************
917 if ( window.ActiveXObject) {
918 var DOMDoc = document;
920 var DOMDoc = Document.prototype;
923 DOMDoc.newXmlDoc = function ( root )
927 if (document.implementation && document.implementation.createDocument) {
928 doc = document.implementation.createDocument('', root, null);
929 } else if ( window.ActiveXObject ) {
930 doc = new ActiveXObject("MSXML2.DOMDocument");
931 doc.loadXML('<' + root + '/>');
933 throw new Error ('No XML support in this browser');
940 DOMDoc.parseXmlFromString = function ( xmlString )
944 if ( window.DOMParser ) {
945 var parser = new DOMParser();
946 doc = parser.parseFromString( xmlString, "text/xml");
947 } else if ( window.ActiveXObject ) {
948 doc = new ActiveXObject("MSXML2.DOMDocument");
949 doc.loadXML( xmlString );
951 throw new Error ("No XML parsing support in this browser.");
957 DOMDoc.transformToDoc = function (xmlDoc, xslDoc)
959 if ( window.XSLTProcessor ) {
960 var proc = new XSLTProcessor();
961 proc.importStylesheet( xslDoc );
962 return proc.transformToDocument(xmlDoc);
963 } else if ( window.ActiveXObject ) {
964 return document.parseXmlFromString(xmlDoc.transformNode(xslDoc));
966 alert( 'Unable to perform XSLT transformation in this browser' );
972 Element_removeFromDoc = function (DOM_Element)
974 DOM_Element.parentNode.removeChild(DOM_Element);
977 Element_emptyChildren = function (DOM_Element)
979 while( DOM_Element.firstChild ) {
980 DOM_Element.removeChild( DOM_Element.firstChild )
984 Element_appendTransformResult = function ( DOM_Element, xmlDoc, xslDoc )
986 if ( window.XSLTProcessor ) {
987 var proc = new XSLTProcessor();
988 proc.importStylesheet( xslDoc );
990 docFrag = proc.transformToFragment( xmlDoc, DOM_Element.ownerDocument );
991 DOM_Element.appendChild(docFrag);
992 } else if ( window.ActiveXObject ) {
993 DOM_Element.innerHTML = xmlDoc.transformNode( xslDoc );
995 alert( 'Unable to perform XSLT transformation in this browser' );
999 Element_appendTextNode = function (DOM_Element, tagName, textContent )
1001 var node = DOM_Element.ownerDocument.createElement(tagName);
1002 var text = DOM_Element.ownerDocument.createTextNode(textContent);
1004 DOM_Element.appendChild(node);
1005 node.appendChild(text);
1010 Element_setTextContent = function ( DOM_Element, textContent )
1012 if (typeof DOM_Element.textContent !== "undefined") {
1013 DOM_Element.textContent = textContent;
1014 } else if (typeof DOM_Element.innerText !== "undefined" ) {
1015 DOM_Element.innerText = textContent;
1017 throw new Error("Cannot set text content of the node, no such method.");
1021 Element_getTextContent = function (DOM_Element)
1023 if ( typeof DOM_Element.textContent != 'undefined' ) {
1024 return DOM_Element.textContent;
1025 } else if (typeof DOM_Element.text != 'undefined') {
1026 return DOM_Element.text;
1028 throw new Error("Cannot get text content of the node, no such method.");
1032 Element_parseChildNodes = function (node)
1035 var hasChildElems = false;
1037 if (node.hasChildNodes()) {
1038 var children = node.childNodes;
1039 for (var i = 0; i < children.length; i++) {
1040 var child = children[i];
1041 if (child.nodeType == Node.ELEMENT_NODE) {
1042 hasChildElems = true;
1043 var nodeName = child.nodeName;
1044 if (!(nodeName in parsed))
1045 parsed[nodeName] = [];
1046 parsed[nodeName].push(Element_parseChildNodes(child));
1051 var attrs = node.attributes;
1052 for (var i = 0; i < attrs.length; i++) {
1053 var attrName = '@' + attrs[i].nodeName;
1054 var attrValue = attrs[i].nodeValue;
1055 parsed[attrName] = attrValue;
1058 // if no nested elements, get text content
1059 if (node.hasChildNodes() && !hasChildElems) {
1060 if (node.attributes.length)
1061 parsed['#text'] = node.firstChild.nodeValue;
1063 parsed = node.firstChild.nodeValue;
1069 /* do not remove trailing bracket */