Skip to content

Commit e994e0e

Browse files
authored
feat: read_xml.textConnection() (#461)
* Adds an S3 method for `read_xml()` for `textConnection()` objects. closes #460
1 parent b05d22a commit e994e0e

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ S3method(read_xml,character)
5050
S3method(read_xml,connection)
5151
S3method(read_xml,raw)
5252
S3method(read_xml,response)
53+
S3method(read_xml,textConnection)
5354
S3method(tree_structure,xml_missing)
5455
S3method(tree_structure,xml_node)
5556
S3method(tree_structure,xml_nodeset)

R/xml_parse.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ read_xml.response <- function(x,
182182
)
183183
}
184184

185+
#' @export
186+
read_xml.textConnection <- function(x,
187+
encoding = "",
188+
...) {
189+
s <- paste(readLines(x), collapse = "\n")
190+
read_xml.character(s, ...)
191+
}
192+
185193
#' Download a HTML or XML file
186194
#'
187195
#' Libcurl implementation of `C_download` (the "internal" download method)

tests/testthat/test-xml_parse.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,11 @@ test_that("read_xml and read_html fail with > 1 input", {
117117
# res <- read_html('<html><head')
118118
# expect_s3_class(res, "xml_document")
119119
#})
120+
121+
test_that("read_xml from a textConnection", {
122+
s <- '<?xml version="1.0" encoding="UTF-8"?>\n<outer>\n<inner>Inner</inner>\n</outer>'
123+
con <- textConnection(s)
124+
x <- read_xml(con)
125+
close(con)
126+
expect_s3_class(x, "xml_document")
127+
})

0 commit comments

Comments
 (0)