XML Module

Simple xml/json conversion tool.

Import

import xml from 'xml';

Methods

toJs(xml) 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

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

Example Response

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

toXml

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

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>"
}

Last updated