Skip to main content

$$xmlparse

Parses an XML and converts it to an object

  • Elements with text and attributes will be converted to objects with a #text field for the text

Usage​

{ 
"$$xmlparse": /* XML string */,
"keep_strings": false /* boolean */,
"cdata_prop_name": "#text" /* string */,
"attr_prefix": "@" /* string */,
"array_tags": [] /* string[] */
}
"$$xmlparse([keep_strings],[cdata_prop_name],[attr_prefix],[array_tags]):{input}"
note

Concrete values in the usage example are default values.

Returns​

object

Arguments​

ArgumentTypeValuesRequired / Default ValueDescription
InputstringYesXML string
keep_stringsbooleanfalse/truefalseDo not try to detect primitive types (e.g. numbers, boolean, etc)
attr_prefixstring"@"Prefix for properties extracted from XML attributes
cdata_prop_namestring"#text"A key for the text/CDATA section (when needed)
array_tagsstring[][]Tag names that will always be parsed as arrays

Examples​

Input

Definition

Output

<root></root>
"$$xmlparse:$"
{
"root": ""
}
<root><hello to="world"><hi /><hi /></hello></root>
"$$xmlparse:$"
{
"root": {
"hello": {
"hi": [
"",
""
],
"@to": "world"
}
}
}
<root></root>
{
"$$xmlparse": "$"
}
{
"root": ""
}
<root><hello to="world"><hi /><hi /></hello></root>
{
"$$xmlparse": "$"
}
{
"root": {
"hello": {
"hi": [
"",
""
],
"@to": "world"
}
}
}
<root><hello to="world"><hi><test>X</test></hi></hello></root>
{
"$$xmlparse": "$"
}
{
"root": {
"hello": {
"hi": {
"test": "X"
},
"@to": "world"
}
}
}
<root><hello to="world"><hi><test>X</test></hi></hello></root>
{
"$$xmlparse": "$",
"array_tags": [
"hi"
]
}
{
"root": {
"hello": {
"hi": [
{
"test": "X"
}
],
"@to": "world"
}
}
}
<root><hello to="world"><hi /><hi /></hello></root>
{
"$$xmlparse": "$",
"array_tags": [
"hi"
]
}
{
"root": {
"hello": {
"hi": [
"",
""
],
"@to": "world"
}
}
}
<root><hello to="2"><hi>true</hi></hello></root>
{
"$$xmlparse": "$",
"keep_strings": true
}
{
"root": {
"hello": {
"hi": "true",
"@to": "2"
}
}
}
<root><hello to="2"><hi>true</hi></hello></root>
{
"$$xmlparse": "$"
}
{
"root": {
"hello": {
"hi": true,
"@to": 2
}
}
}