xmldata
Module xmldata
![](https://bcentral-packageicons.azureedge.net/images/ballerina_xmldata_2.2.0.png)
ballerina/xmldata Ballerina library
Overview
This module provides APIs to perform conversions between XML and JSON/Ballerina records. It provides APIs to get natural representations of data in XML from JSON and to get JSON or Ballerina records data from natural representations of data in XML.
For information on the operations, which you can perform with the xmldata module, see the below Functions.
Functions![](/images/permalink.svg)
fromJson![](/images/permalink.svg)
function fromJson(json jsonValue, JsonOptions options) returns xml?|Error
Converts a JSON object to an XML representation.
json data = { name: "John", age: 30 }; xml? xmlValue = check xmldata:fromJson(data);
Parameters
- jsonValue json - The JSON source to be converted to XML
- options JsonOptions (default {}) - The
xmldata:JsonOptions
record for JSON to XML conversion properties
toJson![](/images/permalink.svg)
function toJson(xml xmlValue, XmlOptions options) returns json|Error
Converts an XML object to its JSON representation.
xml xmlValue = xml `<!-- outer comment -->` + xml `<name>supun</name>`; json jsonValue = check xmldata:toJson(xmlValue);
Parameters
- xmlValue xml - The XML source to be converted to JSON
- options XmlOptions (default {}) - The
xmldata:XmlOptions
record consisting of the configurations for the conversion
Return Type
- json|Error - The JSON representation of the given XML on success, else returns an
xmldata:Error
toRecord![](/images/permalink.svg)
function toRecord(xml xmlValue, boolean preserveNamespaces, typedesc<record {}> returnType) returns returnType|Error
Converts an XML to its Record representation.
type Person record { string name; }; xml xmlValue = xml `<!-- outer comment -->` + xml `<name>Alex</name>`; Person|xmldata:Error person = xmldata:toRecord(xmlValue);
Parameters
- xmlValue xml - The XML source to be converted to a Record
- preserveNamespaces boolean (default true) - Instructs whether to preserve the namespaces of the XML when converting
- returnType typedesc<record {}> (default <>) - The
typedesc
of the record that should be returned as a result
Return Type
- returnType|Error - The Record representation of the given XML on success, else returns an
xmldata:Error
Records![](/images/permalink.svg)
xmldata: JsonOptions![](/images/permalink.svg)
Represents a record type to provide configurations for the JSON to XML conversion.
Fields
- attributePrefix string(default "@") - The attribute prefix to use in the XML representation
- arrayEntryTag string(default "item") - The XML tag to add an element from a JSON array
xmldata: XmlOptions![](/images/permalink.svg)
Provides configurations for converting XML to JSON.
Fields
- attributePrefix string(default "@") - Attribute prefix used in the XML
- preserveNamespaces boolean(default true) - Instructs whether to preserve the namespaces of the XML when converting
Errors![](/images/permalink.svg)
xmldata: Error![](/images/permalink.svg)
Represents the error type of the ballerina/xmldata module. This error type represents any error that can occur during the execution of xmldata APIs.
Import
import ballerina/xmldata;
Metadata
Released date: over 2 years ago
Version: 2.2.0
License: Apache-2.0
Compatibility
Platform: java11
Ballerina version: 2201.0.0
GraalVM compatible: Yes
Pull count
Total: 16039
Current verison: 61
Weekly downloads
Keywords
xml
json
Contributors