项目作者: hrbrmstr

项目描述 :
🎩 Automagically Convert XML to JSON an JSON to XML
高级语言: JavaScript
项目地址: git://github.com/hrbrmstr/blackmagic.git
创建时间: 2018-05-17T09:04:47Z
项目社区:https://github.com/hrbrmstr/blackmagic

开源协议:

下载


blackmagic

Automagically Convert ‘XML’ to ‘JSON’/‘JSON’ to ‘XML’

Description

Given a character string of ‘XML’, an ‘xml2’ or ‘XML’ package document,
or a ‘URL’ to retrieve XML content from, convert said ‘XML’ to ‘JSON’
using the ‘xml-js’ ‘npm’ library https://www.npmjs.com/package/xml-js
by Yousuf Almarzooqi. Also handles the reverse (e.g. ‘JSON’ to ‘XML’).

NOTE

Please reconsider your apparent desire to use this package.

Automagic conversion of XML to JSON (or vice-versa) is rarely a good
idea and a path fraught with peril. There are so many options to tweak
to ensure you get what you think you want but likely truly want
something else entirely, such as a more minimal extract of the original
XML file.

Seriously consider parsing the XML/JSON then using purrr idioms to
extract the data you need into a proper list and then call
jsonlite::toJSON() or xml2::as_xml_document() on said list.

What’s Inside The Tin

The following functions are implemented:

  • xml_to_json: Convert XML to JSON
  • json_to_xml: Convert JSON to XML

Installation

  1. devtools::install_github("hrbrmstr/blackmagic")

Usage

  1. library(blackmagic)
  2. # current verison
  3. packageVersion("blackmagic")
  1. ## [1] '0.1.0'

Sample (defaults)

  1. xml <- '<?xml version="1.0" encoding="utf-8"?>
  2. <note importance="high" logged="true">
  3. <title>Happy</title>
  4. <todo>Work</todo>
  5. <todo>Play</todo>
  6. </note>'
  7. cat(xml_to_json(xml))
  1. ## {"declaration":{"attributes":{"version":"1.0","encoding":"utf-8"}},"elements":[{"type":"element","name":"note","attributes":{"importance":"high","logged":"true"},"elements":[{"type":"element","name":"title","elements":[{"type":"text","text":"Happy"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Work"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Play"}]}]}]}

Sample (xml2)

  1. cat(xml_to_json(xml2::read_xml(xml)))
  1. ## {"declaration":{"attributes":{"version":"1.0","encoding":"UTF-8"}},"elements":[{"type":"element","name":"note","attributes":{"importance":"high","logged":"true"},"elements":[{"type":"element","name":"title","elements":[{"type":"text","text":"Happy"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Work"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Play"}]}]}]}

Sample (XML)

  1. cat(xml_to_json(XML::xmlParse(xml)))
  1. ## {"declaration":{"attributes":{"version":"1.0","encoding":"utf-8"}},"elements":[{"type":"element","name":"note","attributes":{"importance":"high","logged":"true"},"elements":[{"type":"element","name":"title","elements":[{"type":"text","text":"Happy"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Work"}]},{"type":"element","name":"todo","elements":[{"type":"text","text":"Play"}]}]}]}

Sample (URL + saner tweaks)

  1. cat(xml_to_json("https://httpbin.org/xml", spaces = 2, compact = FALSE, ignoreDeclaration = TRUE))
  1. ## {
  2. ## "elements": [
  3. ## {
  4. ## "type": "comment",
  5. ## "comment": " A SAMPLE set of slides "
  6. ## },
  7. ## {
  8. ## "type": "element",
  9. ## "name": "slideshow",
  10. ## "attributes": {
  11. ## "title": "Sample Slide Show",
  12. ## "date": "Date of publication",
  13. ## "author": "Yours Truly"
  14. ## },
  15. ## "elements": [
  16. ## {
  17. ## "type": "comment",
  18. ## "comment": " TITLE SLIDE "
  19. ## },
  20. ## {
  21. ## "type": "element",
  22. ## "name": "slide",
  23. ## "attributes": {
  24. ## "type": "all"
  25. ## },
  26. ## "elements": [
  27. ## {
  28. ## "type": "element",
  29. ## "name": "title",
  30. ## "elements": [
  31. ## {
  32. ## "type": "text",
  33. ## "text": "Wake up to WonderWidgets!"
  34. ## }
  35. ## ]
  36. ## }
  37. ## ]
  38. ## },
  39. ## {
  40. ## "type": "comment",
  41. ## "comment": " OVERVIEW "
  42. ## },
  43. ## {
  44. ## "type": "element",
  45. ## "name": "slide",
  46. ## "attributes": {
  47. ## "type": "all"
  48. ## },
  49. ## "elements": [
  50. ## {
  51. ## "type": "element",
  52. ## "name": "title",
  53. ## "elements": [
  54. ## {
  55. ## "type": "text",
  56. ## "text": "Overview"
  57. ## }
  58. ## ]
  59. ## },
  60. ## {
  61. ## "type": "element",
  62. ## "name": "item",
  63. ## "elements": [
  64. ## {
  65. ## "type": "text",
  66. ## "text": "Why "
  67. ## },
  68. ## {
  69. ## "type": "element",
  70. ## "name": "em",
  71. ## "elements": [
  72. ## {
  73. ## "type": "text",
  74. ## "text": "WonderWidgets"
  75. ## }
  76. ## ]
  77. ## },
  78. ## {
  79. ## "type": "text",
  80. ## "text": " are great"
  81. ## }
  82. ## ]
  83. ## },
  84. ## {
  85. ## "type": "element",
  86. ## "name": "item"
  87. ## },
  88. ## {
  89. ## "type": "element",
  90. ## "name": "item",
  91. ## "elements": [
  92. ## {
  93. ## "type": "text",
  94. ## "text": "Who "
  95. ## },
  96. ## {
  97. ## "type": "element",
  98. ## "name": "em",
  99. ## "elements": [
  100. ## {
  101. ## "type": "text",
  102. ## "text": "buys"
  103. ## }
  104. ## ]
  105. ## },
  106. ## {
  107. ## "type": "text",
  108. ## "text": " WonderWidgets"
  109. ## }
  110. ## ]
  111. ## }
  112. ## ]
  113. ## }
  114. ## ]
  115. ## }
  116. ## ]
  117. ## }

Sample (some saner tweaks)

  1. '<?xml version="1.0" encoding="UTF-8"?>
  2. <bookstore>
  3. <book category="cooking">
  4. <title lang="en">Everyday Italian</title>
  5. <author>Giada De Laurentiis</author>
  6. <year>2005</year>
  7. <price>30.00</price>
  8. </book>
  9. <book category="children">
  10. <title lang="en">Harry Potter</title>
  11. <author>J K. Rowling</author>
  12. <year>2005</year>
  13. <price>29.99</price>
  14. </book>
  15. <book category="web">
  16. <title lang="en">Learning XML</title>
  17. <author>Erik T. Ray</author>
  18. <year>2003</year>
  19. <price>39.95</price>
  20. </book>
  21. </bookstore>' -> books
  22. cat(xml_to_json(books, spaces = 2, compact = TRUE, ignoreDeclaration = TRUE))
  1. ## {
  2. ## "bookstore": {
  3. ## "book": [
  4. ## {
  5. ## "_attributes": {
  6. ## "category": "cooking"
  7. ## },
  8. ## "title": {
  9. ## "_attributes": {
  10. ## "lang": "en"
  11. ## },
  12. ## "_text": "Everyday Italian"
  13. ## },
  14. ## "author": {
  15. ## "_text": "Giada De Laurentiis"
  16. ## },
  17. ## "year": {
  18. ## "_text": "2005"
  19. ## },
  20. ## "price": {
  21. ## "_text": "30.00"
  22. ## }
  23. ## },
  24. ## {
  25. ## "_attributes": {
  26. ## "category": "children"
  27. ## },
  28. ## "title": {
  29. ## "_attributes": {
  30. ## "lang": "en"
  31. ## },
  32. ## "_text": "Harry Potter"
  33. ## },
  34. ## "author": {
  35. ## "_text": "J K. Rowling"
  36. ## },
  37. ## "year": {
  38. ## "_text": "2005"
  39. ## },
  40. ## "price": {
  41. ## "_text": "29.99"
  42. ## }
  43. ## },
  44. ## {
  45. ## "_attributes": {
  46. ## "category": "web"
  47. ## },
  48. ## "title": {
  49. ## "_attributes": {
  50. ## "lang": "en"
  51. ## },
  52. ## "_text": "Learning XML"
  53. ## },
  54. ## "author": {
  55. ## "_text": "Erik T. Ray"
  56. ## },
  57. ## "year": {
  58. ## "_text": "2003"
  59. ## },
  60. ## "price": {
  61. ## "_text": "39.95"
  62. ## }
  63. ## }
  64. ## ]
  65. ## }
  66. ## }

The other way ’round

  1. cat(json_to_xml(jsonlite::toJSON(head(mtcars, 2)), spaces=2))
  1. ## <0>
  2. ## <mpg>21</mpg>
  3. ## <cyl>6</cyl>
  4. ## <disp>160</disp>
  5. ## <hp>110</hp>
  6. ## <drat>3.9</drat>
  7. ## <wt>2.62</wt>
  8. ## <qsec>16.46</qsec>
  9. ## <vs>0</vs>
  10. ## <am>1</am>
  11. ## <gear>4</gear>
  12. ## <carb>4</carb>
  13. ## <_row>Mazda RX4</_row>
  14. ## </0>
  15. ## <1>
  16. ## <mpg>21</mpg>
  17. ## <cyl>6</cyl>
  18. ## <disp>160</disp>
  19. ## <hp>110</hp>
  20. ## <drat>3.9</drat>
  21. ## <wt>2.875</wt>
  22. ## <qsec>17.02</qsec>
  23. ## <vs>0</vs>
  24. ## <am>1</am>
  25. ## <gear>4</gear>
  26. ## <carb>4</carb>
  27. ## <_row>Mazda RX4 Wag</_row>
  28. ## </1>

Code of Conduct

Please note that this project is released with a Contributor Code of
Conduct
. By participating in this project you agree to
abide by its terms.