Last active
April 13, 2019 11:59
-
-
Save 174n/913c92b71bec505540db697b17f3f85f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const json2xml = json => | |
Object.keys(json) | |
.map(k => | |
typeof json[k] === "object" && k !== "_attr" | |
? `<${k + | |
(json[k]["_attr"] | |
? " " + Object.keys(json[k]["_attr"]).map( | |
a => `${a}="${json[k]["_attr"][a]}"` | |
).join(" ") | |
: "") | |
}>${json2xml(json[k])}</${k}>` | |
: typeof json[k] === "string" | |
? `<${k}>${json[k]}</${k}>` | |
: "" | |
).join(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const json2xml=t=>Object.keys(t).map(o=>"object"==typeof t[o]&&"_attr"!==o?`<${o+(t[o]._attr?" "+Object.keys(t[o]._attr).map($=>`${$}="${t[o]._attr[$]}"`).join(" "):"")}>${json2xml(t[o])}</${o}>`:"string"==typeof t[o]?`<${o}>${t[o]}</${o}>`:"").join(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const json2xml=t=>Object.keys(t).map(o=>"object"==typeof t[o]&&"_attr"!==o?`<${o+(t[o]._attr?" "+Object.keys(t[o]._attr).map($=>`${$}="${t[o]._attr[$]}"`).join(" "):"")}>${json2xml(t[o])}</${o}>`:"string"==typeof t[o]?`<${o}>${t[o]}</${o}>`:"").join("");export default json2xml; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: