Converting JSON to XML with jQuery

I need to convert JSON to XML (using jquery), but like this:

If my JSON looks like this:

{
name: "sampleConfiguration",
description: "Configuration used for demonstration purposes"
version: "1.0",
parameters: [
    {
        name: "sampleParameter",
        description: "parameter description",
        value: "20",
        enabled: "1"
    },
    {
        name: "items",
        description: "parameter with subparameters",
        value:[
            {
                name: "item",
                description: "nested parameter",
                value: "13"
            },
            {
                name: "item",
                description: "nested parameter 2",
                value: "TEST"
            }
        ]
    }
]}

      

Then I have to convert it to XML like this:

<?xml version="1.0"?>
<sampleConfiguration version="1.0" description="Configuration used for demonstration purposes">
<params>
    <sampleParameter enabled="1" description="parameter description">20</sampleParameter>
    <items description="parameter with subparameters">
        <item description="nested parameter">13</item>
        <item description="nested parameter 2">TEST</item>
    </items>
</params>
</sampleConfiguration>

      

I have searched for JSON to XML conversion code, but I always get XML similar to this (which I don't need):

<?xml version="1.0"?>
<name>sampleConfiguration</name>
<description>Configuration used for demonstration purposes</description>
<version>1.0</version>
<parameters>
   <name>sampleParameter</name>
   <description>parameter description</description>
   <value>20</value>
   <enabled>1</enabled>
</parameters>
<parameters>
   <name>items</name>
   <description>parameter with subparameters</description>
   <value>
      <name>item</name>
      <description>nested parameter</description>
      <value>13</value>
   </value>
   <value>
      <name>item</name>
      <description>nested parameter 2</description>
      <value>TEST</value>
   </value>
</parameters>

      

+3


source to share


1 answer


I think you are looking for: -

http://code.google.com/p/x2js/



This library provides XML to JSON (JavaScript objects) and vice versa for JavaScript transformation. The library is very small and doesn't require any other additional libraries. :)

0


source







All Articles