import ballerina/io;function main (string[] args) {
json j1 = {"Store":{
"@id":"AST",
"name":"Anne",
"address":{
"street":"Main",
"city":"94"
},
"codes":["4", "8"]
}
};
var x1 = j1.toXML({});
io:println(x1);
json j2 = {"Store":{
"#id":"AST",
"name":"Anne",
"address":{
"street":"Main",
"city":"94"
},
"codes":["4", "8"]
}
};
var x2 = j2.toXML({attributePrefix:"#", arrayEntryTag:"wrapper"});
io:println(x2);
}
JSON To XML ConversionJSON to XML conversion can be done using the `toXML` function. This function takes a struct of options as an argument. The options struct has two fields, the `attributePrefix` and the `arrayEntryTag`. The `attributePrefix` is a prefix used to identify the XML attributes. The default value of the `attributePrefix` option is `@`. The `arrayEntryTag` is the name of the tag that is to be added to each entry of the JSON array. The default value of the `arrayEntryTag` option is `item`. |
|
import ballerina/io;
|
|
function main (string[] args) {
|
|
json j1 = {"Store":{
"@id":"AST",
"name":"Anne",
"address":{
"street":"Main",
"city":"94"
},
"codes":["4", "8"]
}
};
|
Create a JSON object. |
var x1 = j1.toXML({});
io:println(x1);
|
Convert the JSON object to XML using the default |
json j2 = {"Store":{
"#id":"AST",
"name":"Anne",
"address":{
"street":"Main",
"city":"94"
},
"codes":["4", "8"]
}
};
var x2 = j2.toXML({attributePrefix:"#", arrayEntryTag:"wrapper"});
io:println(x2);
}
|
Convert the JSON object to XML using a custom |
$ ballerina run json-to-xml-conversion.bal
<Store id="AST"><name>Anne</name><address><street>Main</street><city>94</city>
</address><codes><item>4</item><item>8</item></codes></Store>
<Store id="AST"><name>Anne</name><address><street>Main</street><city>94</city>
</address><codes><wrapper>4</wrapper><wrapper>8</wrapper></codes></Store>
|
At the command line, navigate to the directory that contains
the |