Transforming a JavaScript application from JSON format to XML

Currently, I am in the process of converting a JavaScript program that utilizes JSON files to one that uses XML files for the settings. The structure of the XML files differs from that of the JSON files, and certain JSON variables need to be hardcoded in the program. To avoid recoding all calls to the JSON data, my approach involves creating objects resembling the JSON data and implementing custom functions to load the XML data into these objects when necessary.

Prior to this project, I had no experience working with JSON. Any suggestions on a more efficient solution are welcome.

For simple JSON files, it has been relatively straightforward to create a corresponding class.


{
"logo": "yes",
"title": "Jordan Pond",
"author": "Matthew Petroff",    
"license": 1,    
"preview": "../examples/examplepano-preview.jpg",
}

This transforms into:


function config(){
    this.logo='yes';
    this.title='Jordan Pond';
    this.author='Matthew Petroff';
    this.license='1';
    this.preview='./examples/examplepano-preview.jpg';
}
config = new config();
alert(config.title);

By following this method, all existing calls to the JSON data can proceed as usual. However, I have encountered challenges when dealing with more complex JSON files like:


 {
"default": {
    "license": 0,
    "logo": "no",
    "author": "Kvtours.com",
    "firstScene": "WilsonRiverFishingHole",
    "title": "Wilson"
},
"scenes": {
    "pond": {
        "title": "Jordan Pond",
        "preview": "../examples/examplepano-preview.jpg",
        "panorama": "../examples/examplepano.jpg"
    }
}
}

I anticipated that this would convert to something along the lines of:


function tourConfig(){
this.default= function() {
    license= "0";
    logo= "no";
    author= "Kvtours.com";
    firstScene= "pondCube";
    title= "Wilson";
}
this.scenes= function(){
    this.pondCube= function() {
        this.title= "Jordan Pond (Cube)";
        this.preview="examples/examplepano-preview.jpg";
        this.panorama="../examples/examplepano.jpg";
    }
}
}
tourConfig = new tourConfig();
alert(tourConfig.default.author);

However, I am encountering issues making this work as expected. Does anyone have any thoughts or suggestions on how to resolve this?

Answer №1

You seem to be struggling with understanding javascript closure.

Here is a possible solution:

function configurationSetup(){
    var self = this;
    this.default= function() {
        self.license= "0";
        self.logo= "no";
        self.author= "ExampleSite.com";
        self.startingPoint= "homePage";
        self.title= "Sample Title";
    }
    // more methods can be added here
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Press the jQuery button and inform me once it has been activated

My jQuery code is set up to automatically press buttons for me every second. Occasionally, some pages take a long time to load until the button appears. Here is the current code I am using: (function($){ setInterval(function(){ $('.play-b ...

Issue with Angular 2 pipe causing unexpected undefined result

Here is a JSON format that I am working with: [{ "id": 9156, "slug": "chicken-seekh-wrap", "type": "dish", "title": "Chicken Seekh Wrap", "cuisine_type": [2140] }, { "id": 9150, "slug": "green-salad", "type": "dish", "title": "Green Sala ...

Ensure that the JSON values in the response body are validated thoroughly prior to being sent to the Spring controller in order to prevent

My Spring controller is set up to accept application json response body as an object parameter. However, I am struggling to find a way to intercept the json data before it reaches the controller in order to validate the values. This becomes problematic whe ...

Creating a universal function to handle setTimeout and setInterval globally, inclusive of clearTimeout and clearInterval for all functions

Is it possible to create a universal setTimeout and setInterval function with corresponding clearTimeout and clearInterval for all functions while passing values to them? The situation is as follows: 1. More than 8 functions utilizing setInterval for act ...

Step-by-step guide on retrieving news_id and displaying it in an alert when an item

How can I retrieve the item ID when it is clicked in a Listview to display the specific news_id in an alert? Below is the HTML code snippet for my page: <body> <div data-role="page" id="taxmanhomepage" data-theme="e"> <div data-role="h ...

Error encountered with the $get method of AngularJS Provider during configuration() function

Recently, I configured a Provider that fetches a language JSON file from a web server and delivers it to Angular within the config() method. Here is the provider setup: .provider('language', function () { var languageWsURL , languageC ...

Obtain individual information from XML

After fetching data from the server using ajax, I am looking to retrieve only a specific part of the data which is the name. How can I modify my code to achieve this? const url = 'https://jsonplaceholder.typicode.com/users' const xhr = new XMLH ...

No results found using AngularJS UI-Bootstrap Typeahead

I've been searching around but haven't found any questions related to this topic. Currently, I am utilizing Angular UI-bootstrap typeahead feature. Here is what I have implemented: <input type="text" ng-model="loc" typeahead="location for l ...

Angular 9: Chart.js: Monochromatic doughnut chart with various shades of a single color

My goal is to display a monochromatic doughnut chart, with each segment shaded in varying tones of the same color. I have all the necessary graph data and just need to implement the color shading. ...

Utilizing Angular's ng-repeat with varying directives for each iteration

I am trying to utilize ng-repeat within Angular to iterate through multiple <li> elements with a directive embedded. My goal is to have the first two items in the ng-repeat display differently in terms of styling and content compared to the remaining ...

Tips for utilizing pre-installed validation regulations in VeeValidate 4?

Currently transitioning from Vue 2 to Vue 3, I am interested in utilizing VeeValidate 4 for my validations. However, it seems that the documentation does not provide any information on using built-in rules such as min, max, alpha_spaces, etc. Do I need to ...

Encountering issues with installing packages while creating a new Angular 9 project

Recently I updated to node version 12.16.1 (LTS) and Angular CLI version 9.0.3. After creating a new project with the CLI, all files in the root folder are generated but it gets stuck during the installation of node packages. Has anyone else encountered t ...

The HTML code may fade away, but the JavaScript is still up and running behind the

Switching between different div elements in my HTML document is a challenge. Here's the code I currently have: <div id="screen1" class="current"> <div id="press_any_key_to_continue"> <font style="font-family: verdana" color="yellow ...

Is it possible to detect individual key value modifications in Firestore using Angular?

getUserEventSummaryE(userId) { return docData(doc(this.firestore, `/user/${userId}/event_summary/current/`), {idField : 'playing_summary'}). pipe(distinctUntilChanged((prev, curr) => _.isEqual(prev, curr))); } getUserEventSummaryE functio ...

Counting string properties from imported JSON in Angular 2 can be achieved by iterating through the

What is the easiest method to count a specific attribute in imported json data? I am attempting to calculate the number of instances where the status is "success". I have experimented with pipes but am uncertain about their usage. myjson: { companyA ...

Utilizing Dynamic Image Sources in Vue.js with the Help of APIs

Can someone help me figure out how to solve this issue? I have an API that returns a base64 image, and I want to load this image on my site. Any suggestions on where or how I should implement my function? This is the API call located in the methods: metho ...

Working with ng-model on an array with multiple dimensions

Currently, I am working on storing data in AngularJS. My table consists of various sections, rows, and columns. In each field, there is a dropdown list with the options "O", "T" or "E". My goal is to store these values in an array format: [section][row][c ...

Transmitting an Array Using an Ajax Call

Is there anyone knowledgeable in Ajax here? I'm struggling with sending a javascript array in an ajax request. Can someone provide me with an example of how the ajax request should be formatted? My goal is to gather all the javascript data, package it ...

What is the best way to use ajax for uploading multiple images at once?

I'm having trouble uploading multiple image files. Can someone please review my code? <form id="fileupload" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="multiple" id="images_input"> & ...

Is the design falling short on larger screens, failing to optimize the additional space?

I thought I was almost done with my app, but then I encountered a major bug. The issue arose when testing my app on a Galaxy Nexus, which has a larger screen than the Desire HD that I had been using for testing. Surprisingly, the app looked great on the De ...