Search…
XML Module
Simple xml/json conversion tool.
Import
1
import xml from 'xml';
Copied!

Methods

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
1
import xml from 'xml'
2
3
let xmlString =
4
`<note>
5
<to>John</to>
6
<from>Jane</from>
7
<heading>Reminder</heading>
8
<body>Don't forget me this weekend!</body>
9
</note>`
10
11
return xml.toJs(xmlString)
Copied!
toJS
Example Response
1
{
2
"object": "result",
3
"data": {
4
"note": {
5
"body": [
6
"Don't forget me this weekend!"
7
],
8
"from": [
9
"Jane"
10
],
11
"heading": [
12
"Reminder"
13
],
14
"to": [
15
"John"
16
]
17
}
18
}
19
}
Copied!
toXml
Example
1
import xml from 'xml'
2
import logger from 'logger'
3
4
let jsonString =
5
`{
6
"employee": [
7
{
8
"id": "1",
9
"firstName": "Tom",
10
"lastName": "Cruise",
11
"photo": "https://pbs.twimg.com/profile_images/735509975649378305/B81JwLT7.jpg"
12
}
13
]
14
}`
15
16
17
return xml.toXml(jsonString, {rootElement:'employees'})
Copied!
toXml
Example Response
1
{
2
"object": "result",
3
"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>"
4
}
Copied!
Last modified 2mo ago