Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:08:41+00:00 2026-06-06T23:08:41+00:00

file: dojo/dir1/utils/XmlJsonUtils.js // Author: Rajat Khandelwal define([ dojo/_base/declare // declare ], function(declare){ return declare(dir1.utils.XmlJsonUtils,[],{

  • 0

file: dojo/dir1/utils/XmlJsonUtils.js

// Author: Rajat Khandelwal

define([
    "dojo/_base/declare" // declare
    ], function(declare){
      return declare("dir1.utils.XmlJsonUtils",[],{
        parseXml : function (xml) {
                     var self=this;
                     var dom = null;
                     if (window.DOMParser) {
                       try { 
                         dom = (new DOMParser()).parseFromString(xml, "text/xml"); 
                       } 
                       catch (e) { dom = null; }
                     }
                     else if (window.ActiveXObject) {
                       try {
                         dom = new ActiveXObject('Microsoft.XMLDOM');
                         dom.async = false;
                         if (!dom.loadXML(xml)) // parse error ..

        window.alert(dom.parseError.reason + dom.parseError.srcText);
                       } 
                       catch (e) { dom = null; }
                     }
                     else
                       alert("cannot parse xml string!");
                     return dom;
                   },
                 xml2json  : function (xmldata)
                 {
                   var self=this;
                   if(xmldata.firstChild==null)
                   {
                     return {name:xmldata.nodeName+": (value null)", checked: true}
                   }
                   else if(xmldata.firstChild.nodeType==3)
                   {
                     return {name:xmldata.nodeName+": "+xmldata.firstChild.nodeValue, checked:true}
                   }
                   else
                   {
                     var mychildren=[];
                     var i=0;
                     var nochildren=xmldata.childElementCount
                       for(i=0;i<nochildren;i++)
                       {
                         var j=self.xml2json(xmldata.childNodes[i])
                           mychildren[i]=j
                       }
                     var ret= {name:xmldata.nodeName, children:mychildren, checked:true}
                     return ret
                   }
                 },

                 convert2arr : function (result,ctr,res_arr)
                 {
                   var self=this;
                   if(result[ctr].checked[0]==false)
                     return;
                   if(result[ctr].children==undefined)
                   {
                     var name=result[ctr]['name'][0];
                     var kv = name.split(': ');
                     if(kv[1]=="(value null)")
                       kv[1]="";
                     res_arr.push.apply(res_arr,["<",kv[0],">",kv[1],"</",kv[0],">"]);
                     return ctr+1;
                   }
                   else
                   {
                     var i=ctr;
                     var new_ctr=ctr;
                     var no_children=result[ctr].children.length;
                     res_arr.push.apply(res_arr,["<",result[ctr].name[0],">"])
                       for(i=0;i<no_children;i++)
                       {
                         new_ctr=self.convert2arr(result,result[ctr].children[i]._0,res_arr)
                       }
                     res_arr.push.apply(res_arr,["</",result[ctr].name[0],">"]);
                     return new_ctr;
                   }
                 },
                 convert2xml : function (result)
                 {
                   var arr=[]
                     self.convert2arr(result, 0, arr)
                     return arr.join('')
                 }
      })
    })

but when in the code I require the dir1.utils.XmlJsonUtils, it says Uncaught Error: declare XmlJsonUtils: base class is not a callable constructor. What is the correct way to declare some utility functions.

And those should be like static functions. I don’t want to do x=new XmlJsonUtils(); x.parseXml(..). I want to do XmlJsonUtils.parseXml(..)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:08:43+00:00Added an answer on June 6, 2026 at 11:08 pm

    Your class should not have to have the constructor method defined, dojo.declare is supposed to handle this.. However, doing so doesnt hurt, simply define a blank constructor: function() { }. I suspect youre facing some sort of bug.

    The define is as should be, ‘define’ is used for the require-scope, when running require([“my.module”]), its expected to have a define method, which returns the base class via declare.

    file: dojo/dir1/utils/XmlJsonUtils.js:

    define([
       // requirements
       "dojo/_base/declare", 
       "dir1/utils/Toolkit" // sample in-package dependency
       "./Toolkit"     // Same as Above
    ], function (declare) {
       // no slash separator, use dot with declare, 
       // use a reference and return on last line
       var Klass = declare(
       /// declaredClass: string, moduleUrl with dot-separater + filename /.js//
           "dir1.utils.XmlJsonUtils",
       /// base class: Array(mixins)
           [],
       /// class scope
           {
               _methodMeantToBePrivate: function() { },
               randomInstanceMethod: function() { }
           }
       ); // end declare
    
    
       // set any aliases, which you want to expose (statics)
    
       Klass.StaticCallable = function() {
           // careful with your scope access inhere
       }
    
       // return the declared class to 'define'
       return Klass;
    }); // end define
    

    This way (you must have a reference, either pulled in with require or getObject), you could use the StaticCallable function without initializing / constructing an instance of the module. AMD compliant syntax is like so:

    require(["dir1/utils/XmlJsonUtils"], function(xmlUtils) {
       xmlUtils.StaticCallable();
    });
    

    or if previously required

    var xmlUtils = dojo.getObject("dir1.utils.XmlJsonUtils")
    xmlUtils.StaticCallable();
    

    A specific example could be a versatile class like the following, where both instance and static access is possible. Base class defines ‘tools’, derived class defines the variables the ‘tools’ operate on – and if instantiated, the default topics can be subscribed – [ MessageBusBase | MessageBus ]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I browse to upload a file using Dojo? See the code below:
FILE #1 (foo.h): #ifndef FOO_H_ #define FOO_H_ #include baseclass.h #include bar.h class Bar; class
In the following example, 'xxx.xml' is a valid XML file and 'xxx.txt' is a
I am building Dojo mobile app. I have a Json file like: { Introduction:
I am building Dojo mobile app. I have a Json file like: { Introduction:
I have a js file that is simply just using the dojo provide statement
How can I get Dojo Dijits (1.5.0, currently) to work with XHTML as application/xml+xhtml?
I need the complete code for uploading a file in dojo. Especially the ajax
I have a question about exporting dojo data grid to excel file. I have
I'm trying to do an ajax file upload using dojo.io.frame.send(). It works with FF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.