-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-xml.R
28 lines (24 loc) · 823 Bytes
/
utils-xml.R
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
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Make a distinct, unlinked, independent copy of an xml node
#'
#' Since XML documents and nodes are almost always handled 'by-reference'
#' if you want to keep an original untouched when adding nodes etc,
#' you need to make an unlinked copy of it.
#'
#' @param x xml document
#'
#' @return new, unlinked xml document
#'
#' @import xml2
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xml_duplicate <- function(x) {
chr <- as.character(x)
if (startsWith(chr, "<!DOCTYPE")) {
xml2::read_html(chr)
} else if (startsWith(chr, "<?xml") || startsWith(chr, "<svg")) {
xml2::read_xml(chr)
} else {
message("Not sure if this is XML or HTML. Duplicating as HTML")
xml2::read_html(chr)
}
}