## ---- include = FALSE--------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----setup-------------------------------------------------------------------- library(xmlr) ## ----------------------------------------------------------------------------- doc <- parse.xmlstring("
Apples Bananas
") ## ---- eval = FALSE------------------------------------------------------------ # doc <- parse.xmlfile("pom.xml") ## ----------------------------------------------------------------------------- doc <- Document$new() root <- Element$new("table") root$setAttribute("xmlns", "http://www.w3.org/TR/html4/") doc$setRootElement(root) root$addContent( Element$new("tr") $addContent( Element$new("td")$setAttribute("class", "fruit")$setText("Apples") ) $addContent( Element$new("td")$setAttribute("class", "fruit")$setText("Bananas") ) ) # print it out just to show the content print(doc) ## ----------------------------------------------------------------------------- doc$getRootElement()$getChild("tr")$getChildren()[[2]]$getText() ## ----------------------------------------------------------------------------- groceriesDf <- NULL for ( child in doc$getRootElement()$getChild("tr")$getChildren() ) { row <- list() row[["class"]] <- child$getAttribute("class") row[["item"]] <- child$getText() if (is.null(groceriesDf)) { groceriesDf <- data.frame(row, stringsAsFactors = FALSE) } else { groceriesDf <- rbind(groceriesDf, row) } } groceriesDf ## ----------------------------------------------------------------------------- # print the contents of the tr element root$getChild("tr") ## ----------------------------------------------------------------------------- paste("The root element is", root) ## ----------------------------------------------------------------------------- e <- Element$new() e$setName("foo") ## ----------------------------------------------------------------------------- e <- Element$new("foo")$addContent( Element$new("Bar")$setAttribute("note", "Some text") )$addContent( Element$new("Baz")$setAttribute("note", "More stuff") ) e ## ----------------------------------------------------------------------------- e <- Element$new("car")$setText("Volvo") ## ----------------------------------------------------------------------------- xml <- "Volvo" e <- Element$new("car") e$addContent(Text$new("Volvo")) e$addContent(Element$new("value")$setAttribute("sek", "200000")) stopifnot(e$toString() == xml) ## ----------------------------------------------------------------------------- parent <- Element$new("parent") parent <- Element$new("parent")$setAttribute("xmlns:env", "http://some.namespace.com") parent$addContent(Element$new("env:child")) parent ## ----------------------------------------------------------------------------- xml <- " Apples Bananas Tomatoes " doc <- parse.xmlstring(xml) xmldf <- xmlrToDataFrame(doc$getRootElement()) xmldf ## ----------------------------------------------------------------------------- isRc(Element$new("Hello"), "Element") isRc(Text$new("Hello"), "Element")