Converting JSON data into XML format

When I am converting JSON Values to XML, instead of getting JSON properties as elements of XML, I am receiving

"title":"source"
. The desired output should be
<title>source</title>
. Can you help me identify what mistake I might be making? This code is written in JavaScript.

I am utilizing the x2js plugin for conversion and have included it using a script tag.

The code snippet used for converting dynatree to JSON and then JSON to XML is shown below:

var x2js = new X2JS();

var tree = $("#source").dynatree("getTree").toDict();
alert("  tree:"+tree);
var jsonObject = JSON.stringify(tree); //Convert dynatree to JSON
alert("jsonObject: "+jsonObject);       
var xmlAsStr = x2js.json2xml_str(jsonObject); //Convert JSON to XML
alert("xml: "+xmlAsStr);

Answer №1

Avoid using JSON.stringify(tree); as it will cause the string to be escaped.

Instead, use

var xmlAsStr = x2js.json2xml_str(tree);

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

Implementing a Reset Button to Clear Checkboxes with PHP or JavaScript

I'm looking for help with setting up a button to reset the checkboxes on my preferences page. UPDATEL: Here's more information: The solutions provided should only affect checkboxes that are currently selected, not those enabled onLoad. This is ...

Strategies for sorting data in d3js/dimplejs visualizations

I am looking to enhance the interactivity and responsiveness of a d3js/dimplejs chart by implementing filtering based on clicks in the legends for different series. The code I tried below did not hide the series as expected, although it worked well with a ...

Having Trouble Retrieving Environment Variables in Next.js API Endpoints

Currently, I am in the process of working on a project utilizing Next.js 13 and API routes to communicate with a database. My goal is to securely store my database credentials in a .env file, but unfortunately, the variables are not loading as expected. I ...

Retrieving a variable within a try-catch statement

I am trying to implement a function: function collect_user_balance() { userBalance = 0; try { var args = { param: 'name'; }; mymodule_get_service({ data: JSON.stringify(args), s ...

What is the syntax for creating a link tag with interpolation in Angular 2 / Ionic 2?

As I work on developing an app using Ionic 2/Angular 2, I have encountered a challenge that I am struggling to overcome. Let me provide some context: I am retrieving multiple strings from a webservice, and some of these strings contain links. Here is an e ...

Struggling with implementing both weighted and unweighted quick union algorithms

For a programming project in school, I was tasked with creating a percolation model. However, I encountered a confusing issue along the way. The assignment required us to build an API for running a percolation simulation. public class Percolation{ private ...

How can I restrict the draggable space? It is only working on the top and left sides, but I want to disable it on the right and bottom

Attempting to prevent the draggable items from overflowing beyond the body. Succeeded in limiting movement on the top and left sides. Encountering difficulty restricting motion on the right side and bottom. e.pageX -= e.offsetX; e.pageY -= e.offsetY; ...

The instance does not have a definition for the property or method "foo" that was referenced during rendering

[Vue warn]: Property or method "modelInfo" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. Whenever I ...

Incorporating React.js into HTML DOM Elements

As a beginner in react js, I'm facing an issue regarding DOM elements. Within my component, there is a table. When hovering over a cell, I want to highlight both the corresponding row and cell. Additionally, I need to obtain the coordinates of the hov ...

The element in TS 7023 is implicitly assigned an 'any' type due to the fact that an expression of type 'any' is not valid for indexing in type '{}'

I have implemented a select-box that includes options, labels, optgroups, and values. Is my approach correct or is there something wrong with the way I have defined my types? interface Option { label: string value: string selected?:boolean mainGrou ...

Is it more beneficial to keep a function inside or outside another function if it is only being used within it?

Within the topic at hand, I have a function structured as follows. There are numerous auxiliary functions declared within this function (twice the amount shown in the example) because they are solely utilized by this function. My query is: should I extrac ...

Guide on creating a darkening effect for lights

Seeking assistance on how to create a light-off effect when clicking on any of my textboxes. I discovered this concept on the following website: What I aim to achieve is that upon clicking a specific textbox, it will be highlighted. Upon clicking the sam ...

A Guide on Adding Excel-Like Filtering Functionality to AngularJS

I am struggling to implement a simple Excel-like filter for a table using AngularJS (v.1). I have shared my code snippet below and would appreciate any help to show filtered data in the table after checking a checkbox and clicking on the OK button. I have ...

Can someone please explain why webpack is searching for a .json module when in fact it is an ES6 JSON file being read

Using ES6, a Node app.js reads JSON data from a file for each request (not the optimal scenario but it's necessary). JSON.parse(fs.readFileSync(`../../src/components/${componentName}/mock.props.json`).toString()); Attempted to use: new webpack.Igno ...

Using Python to iterate through a list and pass it as parameters in a MySQL query with a FOR loop

Trying to loop through a Python list for multiple queries in MySQL, but facing issues with the quotes included in the "%s" parameter resulting in only zeros (0) being returned instead of the expected numbers. The code snippet is as follows: def expor ...

Discovering which particular check in the Express Validator is causing the errors can be done by following these steps

I am currently developing a web application that requires an admin user to create new users. The admin user's username and password are stored in the .env file, and I am utilizing the dotenv package for this purpose. However, I am facing an issue when ...

Is there a different option instead of using the exit() function?

I am attempting to modify the class of a button based on a condition not being met (if(strlen($username) < 5)). Here is my code: if( strlen($username) < 5 ){ echo '<span class = "glyphicon glyphicon-remove" style = "color:red"> ...

Inaccessible " following the "Return" Conflict

I'm struggling to resolve these warnings: Merge this with the previous "var" statement, found on lines 7, 9 and 10. Unreachable " after 'Return' - it's related to ($window.width() <= 770) { return; Script snippet: //StickyB ...

Encountering difficulties with displaying error messages on the Material UI datepicker

I am currently utilizing React Material UI There is a need to display an error based on certain conditions that will be calculated on the backend. Although I have implemented Material UI date picker, I am facing issues with displaying errors. import * as ...

Scrape JSON Data and Convert Military Time to Standard Time Using JavaScript

I have a question about scraping JSON data from a URL. The timestamps are in military time and I'm looking for a way to convert them to standard time on the client side. Here is the JSON snippet: [ { SaturdayClose: "21:00", SaturdayOpen: " ...