File: xml_document.R

package info (click to toggle)
r-cran-xml2 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 976 kB
  • sloc: cpp: 1,828; xml: 333; javascript: 238; ansic: 213; sh: 74; makefile: 6
file content (43 lines) | stat: -rw-r--r-- 929 bytes parent folder | download | duplicates (2)
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
xml_document <- function(doc) {
  if (.Call(doc_has_root, doc)) {
    x <- xml_node(.Call(doc_root, doc), doc)
    class(x) <- c("xml_document", class(x))
    x
  } else {
    out <- list(doc = doc)
    class(out) <- "xml_document"
    out
  }
}

doc_type <- function(x) {
  if (is.null(x$doc)) {
    return("xml")
  }
  if (.Call(doc_is_html, x$doc)) {
    "html"
  } else {
    "xml"
  }
}

#' @export
print.xml_document <- function(x, width = getOption("width"), max_n = 20, ...) {
  doc <- xml_document(x$doc)
  cat("{", doc_type(x), "_document}\n", sep = "")
  if (inherits(doc, "xml_node")) {
    cat(format(doc), "\n", sep = "")
    show_nodes(xml_children(doc), width = width, max_n = max_n)
  }
}

#' @export
as.character.xml_document <- function(
  x,
  ...,
  options = "format",
  encoding = "UTF-8"
) {
  options <- parse_options(options, xml_save_options())
  .Call(doc_write_character, x$doc, encoding, options)
}