subsoft
subsoft

Reputation: 99

How to use js2xmlparser to output xml

Hi currently i am using js2xmlparser module to generate XML using node.

I have managed to structure most of the xml as required but not sure how to iterate through Json object as below and format xml as required:

Here is the sample Json object:

Json data format as (data):
{
    Group-type-1:"Value1",
    Group-type-2:"Value2",
    Group-type-3:"Value3",
    Group-type-4:"Value4"
}

Expected XML data format:

<main-group>
    <group>
        <name>Group type 1</name>
        <value>Some value</value>
    </group>
        <group>
        <name>Group type 2</name>
        <value>Some value</value>
    </group>
        <group>
        <name>Group type 3</name>
        <value>Some value</value>
    </group>
        <group>
        <name>Group type 4</name>
        <value>Some value</value>
    </group>
</main-group>

Here is what I have done so far:

parsedBody = JSON.parse(data);

"main-group":{
            "group":{
                "name":"Group type 1",
                "vale":parsedBody["Group-type-1"]
            },
            ...
            ...
            ...
        }

Upvotes: 0

Views: 4512

Answers (2)

Prakarsh Kesharwani
Prakarsh Kesharwani

Reputation: 1

var js2xmlparser = require("js2xmlparser");

var data = {
    "@": {
      "type": "individual"
    },
    "firstName": "Prakarsh",
    "lastName": "Kesharwani",
    "dateOfBirth": new Date(1993, 9, 07),
    "address": {
        "@": {
            "type": "home"
        },
        "streetAddress": "3212 22nd St",
        "city": "Gwalior",
        "state": "MP",
        "zip": 474001
    },
    "phone": [
        {
            "@": {
                "type": "home"
            },
            "#": "123-555-4567"
        },
        {
            "@": {
                "type": "work"
            },
            "#": "789-555-4567"
        },
        {
            "@": {
                "type": "cell"
            },
            "#": "456-555-7890"
        }
    ],
    "email": function() {
        return "[email protected]";
    },
    "notes": "Prakarsh profile is not complete."
};

console.log(js2xmlparser("person", data));

 <?xml version="1.0" encoding="UTF-8"?>
 <person type="individual">
     <firstName>Prakarsh</firstName>
    <lastName>Kesharwani</lastName>
     <dateOfBirth>Mon Sep 07 1993 00:00:00 GMT-0400 (Eastern Daylight Time)</dateOfBirth>
     <address type="home">
         <streetAddress>3212 22nd St</streetAddress>
         <city>Gwalior</city>
         <state>MP</state>
         <zip>474001</zip>
     </address>
     <phone type="home">123-555-4567</phone>
     <phone type="work">789-555-4567</telephone>
     <phone type="cell">456-555-7890</phone>
     <email>[email protected]</email>
    <notes>Prakarsh profile is not complete.</notes>
</person>

 - List item

Upvotes: 0

Michael Kourlas
Michael Kourlas

Reputation: 128

You need to use an array.

The following example uses the latest version of js2xmlparser (2.0.2, at the time of writing):

var js2xmlparser = require("js2xmlparser");
js2xmlparser.parse("main-group", {
    "group": [
        {
            "name": "Group type 1",
            "value": "Some value 1"
        },
        {
            "name": "Group type 2",
            "value": "Some value 2"
        },
        ...
    ]
});

Upvotes: 3

Related Questions