# XML Module

Simple xml/json conversion tool.&#x20;

Import

```
import xml from 'xml';
```

### Methods

[toJs(xml)](https://docs.medable.com/reference#section-tojs-xml-)\
[toXml(json, options)](https://docs.medable.com/reference#section-toxml-json-options-)

### toJs(xml)

Parses xml into a json object.

**Arguments**

* `XML` (String) an xml input string

**Returns**

JSON Object

### toXml(json, options)

Converts a json object to an xml document.

**Arguments**

* `json` (Object) a json input object.
* `options` (Object)
  * `rootElement` (String: root)
  * `prettyPrint` (Boolean: false)
  * `indent` (String: \t)
  * `newline` (String: \n)
  * `version` (String: 1.0)
  * `encoding` (String: UTF-8)
  * `standalone` (Boolean: true)
  * `doctype` (String: null)
  * `headless` (Boolean: false)

**Returns**

XML Document String

### Examples

toJS&#x20;

Example

```
import xml from 'xml'

let xmlString = 
`<note>
    <to>John</to>
    <from>Jane</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>`

return xml.toJs(xmlString)
```

toJS&#x20;

Example Response

```
{
    "object": "result",
    "data": {
        "note": {
            "body": [
                "Don't forget me this weekend!"
            ],
            "from": [
                "Jane"
            ],
            "heading": [
                "Reminder"
            ],
            "to": [
                "John"
            ]
        }
    }
}
```

toXml&#x20;

Example

```
import xml from 'xml'
import logger from 'logger'

let jsonString = 
`{
    "employee": [
        {
            "id": "1",
            "firstName": "Tom",
            "lastName": "Cruise",
            "photo": "https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg"
        }
    ]
}`


return xml.toXml(jsonString, {rootElement:'employees'})
```

toXml&#x20;

Example Response

```
{
    "object": "result",
    "data": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><employees>{\n\t\"employee\": [\n\t\t{\n\t\t\t\"id\": \"1\",\n\t\t\t\"firstName\": \"Tom\",\n\t\t\t\"lastName\": \"Cruise\",\n\t\t\t\"photo\": \"https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg\"\n\t\t}\n\t]\n}</employees>"
}
```
