-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
124 lines (87 loc) · 2.97 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
output: rmarkdown::github_document
editor_options:
chunk_output_type: console
---
# 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
```{r eval=FALSE}
devtools::install_github("hrbrmstr/blackmagic")
```
```{r message=FALSE, warning=FALSE, error=FALSE, include=FALSE}
options(width=120)
```
## Usage
```{r message=FALSE, warning=FALSE, error=FALSE}
library(blackmagic)
# current verison
packageVersion("blackmagic")
```
### Sample (defaults)
```{r}
xml <- '<?xml version="1.0" encoding="utf-8"?>
<note importance="high" logged="true">
<title>Happy</title>
<todo>Work</todo>
<todo>Play</todo>
</note>'
cat(xml_to_json(xml))
```
### Sample (`xml2`)
```{r}
cat(xml_to_json(xml2::read_xml(xml)))
```
### Sample (`XML`)
```{r}
cat(xml_to_json(XML::xmlParse(xml)))
```
### Sample (URL + saner tweaks)
```{r}
cat(xml_to_json("https://httpbin.org/xml", spaces = 2, compact = FALSE, ignoreDeclaration = TRUE))
```
### Sample (some saner tweaks)
```{r}
'<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>' -> books
cat(xml_to_json(books, spaces = 2, compact = TRUE, ignoreDeclaration = TRUE))
```
### The other way 'round
```{r}
cat(json_to_xml(jsonlite::toJSON(head(mtcars, 2)), spaces=2))
```
### With a URL source
```{r}
cat(json_to_xml("https://httpbin.org/uuid", spaces=2))
```
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.