R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("XML-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('XML') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "AssignXMLNode" > > ### * AssignXMLNode > > flush(stderr()); flush(stdout()) > > ### Name: [<-.XMLNode > ### Title: Assign sub-nodes to an XML node > ### Aliases: [<-.XMLNode [[<-.XMLNode > ### Keywords: IO file > > ### ** Examples > > top <- xmlNode("top", xmlNode("next","Some text")) > top[["second"]] <- xmlCDataNode("x <- 1:10") > top[[3]] <- xmlNode("tag",attrs=c(id="name")) > > > > cleanEx(); ..nameEx <- "Doctype-class" > > ### * Doctype-class > > flush(stderr()); flush(stdout()) > > ### Name: Doctype-class > ### Title: Class to describe a reference to an XML DTD > ### Aliases: Doctype-class > ### Keywords: classes > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "Doctype" > > ### * Doctype > > flush(stderr()); flush(stdout()) > > ### Name: Doctype > ### Title: Constructor for DTD reference > ### Aliases: Doctype coerce,Doctype,character-method > ### Keywords: IO > > ### ** Examples > > > > > cleanEx(); ..nameEx <- "SAXMethods" > > ### * SAXMethods > > flush(stderr()); flush(stdout()) > > ### Name: startElement.SAX > ### Title: Generic Methods for SAX callbacks > ### Aliases: startElement.SAX endElement.SAX text.SAX comment.SAX > ### processingInstruction.SAX entityDeclaration.SAX .InitSAXMethods > ### text.SAX,ANY,SAXState-method comment.SAX,ANY,SAXState-method > ### endElement.SAX,ANY,SAXState-method > ### startElement.SAX,ANY,ANY,SAXState-method > ### processingInstruction.SAX,ANY,ANY,SAXState-method > ### entityDeclaration.SAX,ANY,ANY,ANY,ANY,ANY,SAXState-method > ### Keywords: file > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "SAXState-class" > > ### * SAXState-class > > flush(stderr()); flush(stdout()) > > ### Name: SAXState-class > ### Title: A virtual base class defining methods for SAX parsing > ### Aliases: SAXState-class > ### Keywords: classes > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "XMLNode-class" > > ### * XMLNode-class > > flush(stderr()); flush(stdout()) > > ### Name: XMLNode-class > ### Title: Classes to describe an XML node object. > ### Aliases: XMLNode-class XMLInternalNode-class > ### Keywords: classes > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "append.XMLNode" > > ### * append.XMLNode > > flush(stderr()); flush(stdout()) > > ### Name: append.xmlNode > ### Title: Add children to an XML node > ### Aliases: append.xmlNode append.XMLNode > ### Keywords: file IO > > ### ** Examples > > ## Not run: > ##D tmp <- lapply(references, function(i) { > ##D if(!inherits(i, "XMLNode")) > ##D i <- xmlNode("reference", i) > ##D i > ##D }) > ##D r[["references"]] <- append.xmlNode(r[["references"]],tmp) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "asXMLNode" > > ### * asXMLNode > > flush(stderr()); flush(stdout()) > > ### Name: asXMLNode > ### Title: Converts non-XML node objects to XMLTextNode objects > ### Aliases: asXMLNode coerce,XMLInternalNode,XMLNode-method > ### Keywords: file > > ### ** Examples > > # creates an XMLTextNode. > asXMLNode("a text node") a text node > > # unaltered. > asXMLNode(xmlNode("p"))

> > > > cleanEx(); ..nameEx <- "dtdElement" > > ### * dtdElement > > flush(stderr()); flush(stdout()) > > ### Name: dtdElement > ### Title: Gets the definition of an element or entity from a DTD. > ### Aliases: dtdElement dtdEntity > ### Keywords: file > > ### ** Examples > > dtdFile <- system.file("exampleData","foo.dtd", package="XML") > foo.dtd <- parseDTD(dtdFile) > > # Get the definition of the `entry1' element > tmp <- dtdElement("variable", foo.dtd) > xmlAttrs(tmp) $type type CDATA #IMPLIED $unit unit CDATA #IMPLIED $levels levels CDATA #IMPLIED > > tmp <- dtdElement("entry1", foo.dtd) > > # Get the definition of the `img' entity > dtdEntity("img", foo.dtd) NULL > > > > cleanEx(); ..nameEx <- "dtdElementValidEntry" > > ### * dtdElementValidEntry > > flush(stderr()); flush(stdout()) > > ### Name: dtdElementValidEntry > ### Title: Determines whether an XML element allows a particular type of > ### sub-element. > ### Aliases: dtdElementValidEntry.character > ### dtdElementValidEntry.XMLElementContent > ### dtdElementValidEntry.XMLElementDef dtdElementValidEntry.XMLOrContent > ### dtdElementValidEntry.XMLSequenceContent dtdElementValidEntry > ### Keywords: file > > ### ** Examples > > dtdFile <- system.file("exampleData", "foo.dtd",package="XML") > dtd <- parseDTD(dtdFile) > > dtdElementValidEntry(dtdElement("variables",dtd), "variable") [1] TRUE > > > > cleanEx(); ..nameEx <- "dtdIsAttribute" > > ### * dtdIsAttribute > > flush(stderr()); flush(stdout()) > > ### Name: dtdIsAttribute > ### Title: Query if a name is a valid attribute of a DTD element. > ### Aliases: dtdIsAttribute > ### Keywords: file > > ### ** Examples > > dtdFile <- system.file("exampleData", "foo.dtd", package="XML") > foo.dtd <- parseDTD(dtdFile) > > # true > dtdIsAttribute("numRecords", "dataset", foo.dtd) [1] TRUE > > # false > dtdIsAttribute("date", "dataset", foo.dtd) [1] FALSE > > > > cleanEx(); ..nameEx <- "dtdValidElement" > > ### * dtdValidElement > > flush(stderr()); flush(stdout()) > > ### Name: dtdValidElement > ### Title: Determines whether an XML tag is valid within another. > ### Aliases: dtdValidElement > ### Keywords: file > > ### ** Examples > > dtdFile <- system.file("exampleData", "foo.dtd", package="XML") > foo.dtd <- parseDTD(dtdFile) > > # The following are true. > dtdValidElement("variable","variables", dtd = foo.dtd) [1] TRUE > dtdValidElement("record","dataset", dtd = foo.dtd) [1] TRUE > > # This is false. > dtdValidElement("variable","dataset", dtd = foo.dtd) [1] FALSE > > > > cleanEx(); ..nameEx <- "genericSAXHandlers" > > ### * genericSAXHandlers > > flush(stderr()); flush(stdout()) > > ### Name: genericSAXHandlers > ### Title: SAX generic callback handler list > ### Aliases: genericSAXHandlers > ### Keywords: file > > ### ** Examples > > ## Don't show: > # .InitSAXMethods() > names(genericSAXHandlers()) [1] "startElement" "endElement" "comment" [4] "text" "processingInstruction" "entityDeclaration" > names(genericSAXHandlers(inc=c("startElement", "endElement", "text"))) [1] "startElement" "endElement" "text" > names(genericSAXHandlers(ex=c("startElement", "endElement", "text"))) [1] "comment" "processingInstruction" "entityDeclaration" > ## End Don't show > > > > cleanEx(); ..nameEx <- "length.XMLNode" > > ### * length.XMLNode > > flush(stderr()); flush(stdout()) > > ### Name: length.XMLNode > ### Title: Determine the number of children in an XMLNode object. > ### Aliases: length.XMLNode > ### Keywords: file > > ### ** Examples > > doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML")) > r <- xmlRoot(doc, skip=TRUE) > length(r) [1] 33 > # get the last entry > r[[length(r)]] 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 > > > > cleanEx(); ..nameEx <- "libxmlVersion" > > ### * libxmlVersion > > flush(stderr()); flush(stdout()) > > ### Name: libxmlVersion > ### Title: Get the version of the libxml library. > ### Aliases: libxmlVersion > ### Keywords: IO > > ### ** Examples > > ver <- libxmlVersion() > if(is.null(ver)) { + cat("Relly old version of libxml\n") + } else { + if(ver$major > 1) { + cat("Using libxml2\n") + } + } Using libxml2 > > > > cleanEx(); ..nameEx <- "names.XMLNode" > > ### * names.XMLNode > > flush(stderr()); flush(stdout()) > > ### Name: names.XMLNode > ### Title: Get the names of an XML nodes children. > ### Aliases: names.XMLNode > ### Keywords: file > > ### ** Examples > > doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML")) > names(xmlRoot(doc)) [1] "variables" "record" "record" "record" "record" "record" [7] "record" "record" "record" "record" "record" "record" [13] "record" "record" "record" "record" "record" "record" [19] "record" "record" "record" "record" "record" "record" [25] "record" "record" "record" "record" "record" "record" [31] "record" "record" "record" > > r <- xmlRoot(doc) > r[names(r) == "variables"] $variables mpg cyl disp hp drat wt qsec vs am gear carb > > > > cleanEx(); ..nameEx <- "newXMLDoc" > > ### * newXMLDoc > > flush(stderr()); flush(stdout()) > > ### Name: newXMLDoc > ### Title: Create internal XML node or document object > ### Aliases: newXMLDoc newXMLNode > ### Keywords: IO > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "parseDTD" > > ### * parseDTD > > flush(stderr()); flush(stdout()) > > ### Name: parseDTD > ### Title: Read a Document Type Definition (DTD) > ### Aliases: parseDTD > ### Keywords: file IO > > ### ** Examples > > dtdFile <- system.file("exampleData", "foo.dtd",package="XML") > parseDTD(dtdFile) $elements $elements$tmp $elements$variables $elements$record $elements$variable $elements$dataset $elements$entry0 $elements$entry1 $elements$entry2 $elements$entry3 $elements$entry $entities NULL attr(,"class") [1] "ExternalDTD" Entity: line 3: error: SAX.xmlSAX2EntityDecl(img) called while not in subset ^ Entity: line 4: error: SAX.xmlSAX2EntityDecl(foobar) called while not in subset ^ Entity: line 6: error: SAX.xmlSAX2ElementDecl(entry3) called while not in subset ENT entry3 ( (variables | (tmp, x)), (record)* , (a*, b,c,d, (e|f)) , (foo)+ ) > ^ Entity: line 7: error: SAX.xmlSAX2ElementDecl(dataset) called while not in subset ^ Entity: line 9: error: SAX.xmlSAX2ElementDecl(entry1) called while not in subset ^ Entity: line 10: error: SAX.xmlSAX2ElementDecl(entry0) called while not in subset MENT entry0 ( (variables | (tmp, x)), (record)* , (tmp | variable)* , (foo)* ) > ^ Entity: line 11: error: SAX.xmlSAX2ElementDecl(entry2) called while not in subset ^ Entity: line 13: error: SAX.xmlSAX2ElementDecl(tmp) called while not in subset ^ Entity: line 16: error: SAX.xmlSAX2ElementDecl(record) called while not in subset ^ Entity: line 17: error: SAX.xmlSAX2ElementDecl(variable) called while not in subset ^ Entity: line 18: error: SAX.xmlSAX2ElementDecl(variables) called while not in subset ^ Entity: line 21: error: SAX.xmlSAX2ElementDecl(entry) called while not in subset ^ Entity: line 26: error: SAX.xmlSAX2AttributeDecl(name) called while not in subset numRecords CDATA #IMPLIED ^ Entity: line 27: error: SAX.xmlSAX2AttributeDecl(numRecords) called while not in subset source CDATA #IMPLIED ^ Entity: line 28: error: SAX.xmlSAX2AttributeDecl(source) called while not in subset > ^ Entity: line 32: error: SAX.xmlSAX2AttributeDecl(count) called while not in subset > ^ Entity: line 36: error: SAX.xmlSAX2AttributeDecl(id) called while not in subset > ^ Entity: line 41: error: SAX.xmlSAX2AttributeDecl(type) called while not in subset levels CDATA #IMPLIED ^ Entity: line 42: error: SAX.xmlSAX2AttributeDecl(levels) called while not in subset unit CDATA #IMPLIED ^ Entity: line 43: error: SAX.xmlSAX2AttributeDecl(unit) called while not in subset > ^ Entity: line 47: error: SAX.xmlSAX2AttributeDecl(a) called while not in subset c (one | two) #IMPLIED ^ Entity: line 48: error: SAX.xmlSAX2AttributeDecl(c) called while not in subset > ^ > > # Read from text > txt <- readLines(dtdFile) > txt <- paste(txt, "\n", collapse="") > d <- parseDTD(txt, asText=TRUE) > > ## Not run: > ##D url <- "http://www.omegahat.org/XML/DTDs/DatasetByRecord.dtd" > ##D d <- parseDTD(url, asText=FALSE) > ## End(Not run) > > > > cleanEx(); ..nameEx <- "print" > > ### * print > > flush(stderr()); flush(stdout()) > > ### Name: print.XMLAttributeDef > ### Title: Methods for displaying XML objects > ### Aliases: print.XMLAttributeDef print.XMLCDataNode > ### print.XMLElementContent print.XMLElementDef print.XMLEntity > ### print.XMLEntityRef print.XMLNode print.XMLTextNode print.XMLComment > ### print.XMLOrContent print.XMLSequenceContent > ### print.XMLProcessingInstruction > ### Keywords: IO file > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "saveXML" > > ### * saveXML > > flush(stderr()); flush(stdout()) > > ### Name: saveXML > ### Title: Output internal XML Tree > ### Aliases: saveXML saveXML.XMLInternalDocument saveXML.XMLInternalDOM > ### saveXML.XMLNode saveXML.XMLOutputStream > ### Keywords: IO file > > ### ** Examples > > con <- xmlOutputDOM() > con$addTag("author", "Duncan Temple Lang") > con$addTag("address", close=FALSE) > con$addTag("office", "2C-259") > con$addTag("street", "Mountain Avenue.") > con$addTag("phone", close=FALSE) > con$addTag("area", "908", attrs=c(state="NJ")) > con$addTag("number", "582-3217") > con$closeTag() # phone > con$closeTag() # address > > saveXML(con$value(), file="out.xml") > > > > cleanEx(); ..nameEx <- "supportsExpat" > > ### * supportsExpat > > flush(stderr()); flush(stdout()) > > ### Name: supportsExpat > ### Title: Determines which native XML parsers are being used. > ### Aliases: supportsExpat supportsLibxml > ### Keywords: file > > ### ** Examples > > # use Expat if possible, otherwise libxml > fileName <- system.file("exampleData", "mtcars.xml", package="XML") > xmlEventParse(fileName, useExpat = supportsLibxml()) $startElement function (name, atts, ...) { con$addTag(name, attrs = atts, close = FALSE) } $endElement function (name) { con$closeTag(name) } $processingInstruction function (sys, value) { con$addNode(xmlPINode(sys, value)) } $text function (x, ...) { con$addNode(xmlTextNode(x)) } $comment function (x, ...) { xmlCommentNode(x) } $externalEntity function (ctxt, baseURI, sysId, publicId, ...) { cat("externalEntity", ctxt, baseURI, sysId, publicId, "\n") } $entityDeclaration function (name, baseURI, sysId, publicId, notation, ...) { cat("externalEntity", name, baseURI, sysId, publicId, notation, "\n") } $dom function () { con } > > > > cleanEx(); ..nameEx <- "toString" > > ### * toString > > flush(stderr()); flush(stdout()) > > ### Name: toString.XMLNode > ### Title: Creates string representation of XML node > ### Aliases: toString.XMLNode > ### Keywords: file > > ### ** Examples > > x <- xmlRoot(xmlTreeParse(system.file("exampleData", "gnumeric.xml", + package = "XML"))) > > toString(x) [1] " \n \n \n \n Application\n \n \n gnumeric\n \n \n \n \n Author\n \n \n Duncan Temple Lang\n \n \n \n \n \n \n \n \n Sheet1\n \n \n 1\n \n \n 7\n \n \n 1.000000\n \n \n \n \n Helvetica\n \n \n \n \n Helvetica\n \n \n \n \n Helvetica\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n 36524\n \n \n \n \n 4\n \n \n \n \n d\n \n \n \n \n 1\n \n \n \n \n x\n \n \n \n \n sd\n \n \n \n \n 2\n \n \n \n \n 5\n \n \n \n \n s\n \n \n \n \n dsa\n \n \n \n \n 3\n \n \n \n \n as\n \n \n \n \n \n " > > > > cleanEx(); ..nameEx <- "xmlApply" > > ### * xmlApply > > flush(stderr()); flush(stdout()) > > ### Name: xmlApply > ### Title: Applies a function to each of the children of an XMLNode > ### Aliases: xmlApply xmlApply.XMLNode xmlApply.XMLDocument > ### xmlApply.XMLDocumentContent xmlSApply xmlSApply.XMLNode > ### xmlSApply.XMLDocument xmlSApply.XMLDocumentContent > ### Keywords: file > > ### ** Examples > > doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML")) > r <- xmlRoot(doc) > xmlSApply(r[[2]], xmlName) text "text" > > xmlApply(r[[2]], xmlAttrs) $text NULL > > xmlSApply(r[[2]], xmlSize) text 0 > > > > > cleanEx(); ..nameEx <- "xmlAttributeType" > > ### * xmlAttributeType > > flush(stderr()); flush(stdout()) > > ### Name: xmlAttributeType > ### Title: The type of an XML attribute for element from the DTD > ### Aliases: xmlAttributeType > ### Keywords: file > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "xmlAttrs" > > ### * xmlAttrs > > flush(stderr()); flush(stdout()) > > ### Name: xmlAttrs > ### Title: Get the list of attributes of an XML node. > ### Aliases: xmlAttrs xmlAttrs.XMLElementDef xmlAttrs.XMLNode > ### xmlAttrs.XMLInternalNode > ### Keywords: IO file > > ### ** Examples > > fileName <- system.file("exampleData", "mtcars.xml", package="XML") > doc <- xmlTreeParse(fileName) > > xmlAttrs(doc$doc$children[["dataset"]]) name numRecords source "mtcars" "32" "R Project" > > xmlAttrs(doc$doc$children[["dataset"]][["variables"]]) count "11" > > > > cleanEx(); ..nameEx <- "xmlChildren" > > ### * xmlChildren > > flush(stderr()); flush(stdout()) > > ### Name: xmlChildren > ### Title: Gets the sub-nodes within an XMLNode object. > ### Aliases: xmlChildren xmlChildren.XMLNode xmlChildren.XMLInternalNode > ### Keywords: file > > ### ** Examples > > fileName <- system.file("exampleData", "mtcars.xml", package="XML") > doc <- xmlTreeParse(fileName) > names(xmlChildren(doc$doc$children[["dataset"]])) [1] "variables" "record" "record" "record" "record" "record" [7] "record" "record" "record" "record" "record" "record" [13] "record" "record" "record" "record" "record" "record" [19] "record" "record" "record" "record" "record" "record" [25] "record" "record" "record" "record" "record" "record" [31] "record" "record" "record" > > > > cleanEx(); ..nameEx <- "xmlContainsEntity" > > ### * xmlContainsEntity > > flush(stderr()); flush(stdout()) > > ### Name: xmlContainsEntity > ### Title: Checks if an entity is defined within a DTD. > ### Aliases: xmlContainsEntity xmlContainsElement > ### Keywords: file > > ### ** Examples > > dtdFile <- system.file("exampleData", "foo.dtd", package="XML") > foo.dtd <- parseDTD(dtdFile) > > # Look for entities. > xmlContainsEntity("foo", foo.dtd) [1] FALSE > xmlContainsEntity("bar", foo.dtd) [1] FALSE > > # Now look for an element > xmlContainsElement("record", foo.dtd) [1] FALSE > > > > cleanEx(); ..nameEx <- "xmlDOMApply" > > ### * xmlDOMApply > > flush(stderr()); flush(stdout()) > > ### Name: xmlDOMApply > ### Title: Apply function to nodes in an XML tree/DOM. > ### Aliases: xmlDOMApply > ### Keywords: file > > ### ** Examples > > dom <- xmlTreeParse(system.file("exampleData","mtcars.xml", package="XML")) > tagNames <- function() { + tags <- character(0) + add <- function(x) { + if(inherits(x, "XMLNode")) { + if(is.na(match(xmlName(x), tags))) + tags <<- c(tags, xmlName(x)) + } + + NULL + } + + return(list(add=add, tagNames = function() {return(tags)})) + } > > h <- tagNames() > xmlDOMApply(xmlRoot(dom), h$add) NULL > h$tagNames() [1] "text" "variable" "variables" "record" "dataset" > > > > cleanEx(); ..nameEx <- "xmlElementsByTagName" > > ### * xmlElementsByTagName > > flush(stderr()); flush(stdout()) > > ### Name: xmlElementsByTagName > ### Title: Retrieve the children of an XML node with a specific tag name > ### Aliases: xmlElementsByTagName > ### Keywords: IO file > > ### ** Examples > > ## Not run: > ##D doc <- xmlTreeParse("http://www.omegahat.org/Scripts/Data/mtcars.xml") > ##D xmlElementsByTagName(doc$children[[1]], "variable") > ## End(Not run) > > doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML")) > xmlElementsByTagName(xmlRoot(doc)[[1]], "variable") $variable mpg $variable cyl $variable disp $variable hp $variable drat $variable wt $variable qsec $variable vs $variable am $variable gear $variable carb > > > > cleanEx(); ..nameEx <- "xmlEventHandler" > > ### * xmlEventHandler > > flush(stderr()); flush(stdout()) > > ### Name: xmlEventHandler > ### Title: Default handlers for the SAX-style event XML parser > ### Aliases: xmlEventHandler > ### Keywords: file IO > > ### ** Examples > > xmlEventParse(system.file("exampleData", "mtcars.xml", package="XML"), + handlers=xmlEventHandler()) $startElement function (name, atts, ...) { con$addTag(name, attrs = atts, close = FALSE) } $endElement function (name) { con$closeTag(name) } $processingInstruction function (sys, value) { con$addNode(xmlPINode(sys, value)) } $text function (x, ...) { con$addNode(xmlTextNode(x)) } $comment function (x, ...) { xmlCommentNode(x) } $externalEntity function (ctxt, baseURI, sysId, publicId, ...) { cat("externalEntity", ctxt, baseURI, sysId, publicId, "\n") } $entityDeclaration function (name, baseURI, sysId, publicId, notation, ...) { cat("externalEntity", name, baseURI, sysId, publicId, notation, "\n") } $dom function () { con } > > > > cleanEx(); ..nameEx <- "xmlEventParse" > > ### * xmlEventParse > > flush(stderr()); flush(stdout()) > > ### Name: xmlEventParse > ### Title: XML Event/Callback element-wise Parser > ### Aliases: xmlEventParse > ### Keywords: file IO > > ### ** Examples > > fileName <- system.file("exampleData", "mtcars.xml", package="XML") > > # Print the name of each XML tag encountered at the beginning of each > # tag. > # Uses the libxml SAX parser. > xmlEventParse(fileName, + list(startElement=function(name, attrs){cat(name,"\n")}), + useTagName=FALSE, addContext = FALSE) dataset variables variable variable variable variable variable variable variable variable variable variable variable record record record record record record record record record record record record record record record record record record record record record record record record record record record record record record record record $startElement function (name, attrs) { cat(name, "\n") } > > ## Not run: > ##D # Parse the text rather than a file or URL by reading the URL's contents > ##D # and making it a single string. Then call xmlEventParse > ##D xmlURL <- "http://www.omegahat.org/Scripts/Data/mtcars.xml" > ##D xmlText <- paste(scan(xmlURL, what="",sep="\n"),"\n",collapse="\n") > ##D xmlEventParse(xmlText, asText=TRUE) > ## End(Not run) > > # Using a state object to share mutable data across callbacks > f <- system.file("exampleData", "gnumeric.xml", package = "XML") > zz <- xmlEventParse(f, + handlers = list(startElement=function(name, atts,.state) { + state = .state + 1 + print(.state) + .state}), state = 0) [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 [1] 0 > print(zz) [1] 0 > > > if(libxmlVersion()$major >= 2) { + + startElement = function(x, ...) cat(x, "\n") + + xmlEventParse(file(f), handlers = list(startElement = startElement)) + + xmlConnection = + function(con) { + + if(is.character(con)) + con = file(con, "r") + + if(isOpen(con, "r")) + open(con, "r") + + function(len) { + + if(len < 0) { + close(con) + return(character(0)) + } + + x = character(0) + tmp = "" + while(length(tmp) > 0 && nchar(tmp) == 0) { + tmp = readLines(con, 1) + if(length(tmp) == 0) + break + if(nchar(tmp) == 0) + x = append(x, "\n") + else + x = tmp + } + if(length(tmp) == 0) + return(tmp) + + x = paste(x, collapse="") + print(x) + x + } + } + + ff = xmlConnection(f) + xmlEventParse(ff, handlers = list(startElement = startElement)) + } gmr:Workbook gmr:Summary gmr:Item gmr:name gmr:val-string gmr:Item gmr:name gmr:val-string gmr:Geometry gmr:Sheets gmr:Sheet gmr:Name gmr:MaxCol gmr:MaxRow gmr:Zoom gmr:CellStyles gmr:Style gmr:Font gmr:Style gmr:Font gmr:Style gmr:Font gmr:Cols gmr:ColInfo gmr:ColInfo gmr:Rows gmr:RowInfo gmr:RowInfo gmr:RowInfo gmr:RowInfo gmr:RowInfo gmr:RowInfo gmr:RowInfo gmr:Cells gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content gmr:Cell gmr:Content Warning: connection is already open [1] "" [1] "" [1] " " [1] " " [1] " Application" [1] " gnumeric" [1] " " [1] " " [1] " Author" [1] " Duncan Temple Lang" [1] " " gmr:Workbook [1] " " gmr:Summary [1] " " gmr:Item gmr:name [1] " " [1] " " gmr:val-string [1] " Sheet1" [1] " 1" gmr:Item gmr:name [1] " 7" [1] " 1.000000" gmr:val-string [1] " " [1] " " gmr:Geometry gmr:Sheets gmr:Sheet [1] " Helvetica" gmr:Name gmr:MaxCol gmr:MaxRow [1] " " [1] " " gmr:Zoom gmr:CellStyles [1] " Helvetica" gmr:Style [1] " " [1] " " gmr:Font [1] " Helvetica" gmr:Style [1] " " [1] " " gmr:Font [1] " " [1] " " [1] " " gmr:Style gmr:Font [1] " " [1] " " [1] " " gmr:Cols [1] " " gmr:ColInfo [1] " " gmr:ColInfo gmr:Rows [1] " " gmr:RowInfo [1] " " gmr:RowInfo [1] " " gmr:RowInfo [1] " " gmr:RowInfo [1] " " [1] " " [1] " " gmr:RowInfo [1] " 36524" [1] " " [1] " " gmr:RowInfo [1] " 4" [1] " " gmr:RowInfo [1] " " gmr:Cells [1] " d" gmr:Cell [1] " " [1] " " gmr:Content [1] " 1" [1] " " gmr:Cell [1] " " gmr:Content [1] " x" [1] " " gmr:Cell [1] " " gmr:Content [1] " sd" gmr:Cell [1] " " [1] " " gmr:Content [1] " 2" gmr:Cell [1] " " [1] " " gmr:Content [1] " 5" gmr:Cell [1] " " [1] " " gmr:Content [1] " s" [1] " " gmr:Cell [1] " " gmr:Content [1] " dsa" gmr:Cell [1] " " [1] " " gmr:Content [1] " 3" gmr:Cell [1] " " [1] " " gmr:Content [1] " as" gmr:Cell [1] " " [1] " " gmr:Content [1] " " [1] " " [1] "" gmr:Cell gmr:Content gmr:Cell gmr:Content $startElement function (x, ...) cat(x, "\n") > > > > cleanEx(); ..nameEx <- "xmlGetAttr" > > ### * xmlGetAttr > > flush(stderr()); flush(stdout()) > > ### Name: xmlGetAttr > ### Title: Get the value of an attribute in an XML node > ### Aliases: xmlGetAttr > ### Keywords: file > > ### ** Examples > > node <- xmlNode("foo", attrs=c(a="1", b="my name")) > > xmlGetAttr(node, "a") [1] "1" > xmlGetAttr(node, "doesn't exist", "My own default value") [1] "My own default value" > > xmlGetAttr(node, "b", "Just in case") [1] "my name" > > > > cleanEx(); ..nameEx <- "xmlHandler" > > ### * xmlHandler > > flush(stderr()); flush(stdout()) > > ### Name: xmlHandler > ### Title: Example XML Event Parser Handler Functions > ### Aliases: xmlHandler > ### Keywords: file IO > > ### ** Examples > > ## Not run: > ##D xmlURL <- "http://www.omegahat.org/Scripts/Data/mtcars.xml" > ##D xmlText <- paste(scan(xmlURL, what="", sep="\n"),"\n",collapse="\n") > ## End(Not run) > > xmlURL <- system.file("exampleData", "mtcars.xml", package="XML") > xmlText <- paste(readLines(xmlURL), "\n", collapse="") > xmlEventParse(xmlText, handlers = NULL, asText=TRUE) > xmlEventParse(xmlText, xmlHandler(), useTagName=TRUE, asText=TRUE) comment Taken from the R distribution, in turn taken from Henderson and Velleman 1981, Building multiple regression models interactively, Biometrics 37 391-411 . MyText: MyText: MyText: mpg MyText: MyText: cyl MyText: MyText: disp MyText: MyText: hp MyText: MyText: drat MyText: MyText: wt MyText: MyText: qsec MyText: MyText: vs MyText: MyText: am MyText: MyText: gear MyText: MyText: carb MyText: MyText: MyText: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 MyText: MyText: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 MyText: MyText: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 MyText: MyText: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 MyText: MyText: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 MyText: MyText: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 MyText: MyText: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 MyText: MyText: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 MyText: MyText: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 MyText: MyText: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 MyText: MyText: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 MyText: MyText: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 MyText: MyText: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 MyText: MyText: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 MyText: MyText: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 MyText: MyText: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 MyText: MyText: 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 MyText: MyText: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 MyText: MyText: 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 MyText: MyText: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 MyText: MyText: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 MyText: MyText: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 MyText: MyText: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 MyText: MyText: 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 MyText: MyText: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 MyText: MyText: 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 MyText: MyText: 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 MyText: MyText: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 MyText: MyText: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 MyText: MyText: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 MyText: MyText: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 MyText: MyText: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 MyText: $startElement function (name, atts, ...) { if (is.null(atts)) atts <- list() data[[name]] <<- atts } $getData function () { data } $comment function (x, ...) { cat("comment", x, "\n") } $externalEntity function (ctxt, baseURI, sysId, publicId, ...) { cat("externalEntity", ctxt, baseURI, sysId, publicId, "\n") } $entityDeclaration function (name, baseURI, sysId, publicId, notation, ...) { cat("externalEntity", name, baseURI, sysId, publicId, notation, "\n") } $text function (x, ...) { cat("MyText:", x, "\n") } $foo function (x, attrs, ...) { cat("In foo\n") } > > > > cleanEx(); ..nameEx <- "xmlName" > > ### * xmlName > > flush(stderr()); flush(stdout()) > > ### Name: xmlName > ### Title: Extraces the tag name of an XMLNode object. > ### Aliases: xmlName xmlName.XMLComment xmlName.XMLNode > ### xmlName.XMLInternalNode > ### Keywords: file > > ### ** Examples > > fileName <- system.file("exampleData", "test.xml", package="XML") > doc <- xmlTreeParse(fileName) > xmlName(xmlRoot(doc)[[1]]) [1] "element" > > > > cleanEx(); ..nameEx <- "xmlNamespace" > > ### * xmlNamespace > > flush(stderr()); flush(stdout()) > > ### Name: xmlNamespace > ### Title: Retrieve the namespace value of an XML node. > ### Aliases: xmlNamespace xmlNamespace.XMLNode xmlNamespace.XMLInternalNode > ### Keywords: file > > ### ** Examples > > doc <- xmlTreeParse(system.file("exampleData", "job.xml", package="XML")) > xmlNamespace(xmlRoot(doc)) [1] "gjob" > xmlNamespace(xmlRoot(doc)[[1]][[1]]) [1] "gjob" > > node <- xmlNode("arg", xmlNode("name", "foo"), namespace="R") > xmlNamespace(node) [1] "R" > > > > cleanEx(); ..nameEx <- "xmlNode" > > ### * xmlNode > > flush(stderr()); flush(stdout()) > > ### Name: xmlNode > ### Title: Create an XML node > ### Aliases: xmlNode xmlTextNode xmlPINode xmlCDataNode xmlCommentNode > ### Keywords: file > > ### ** Examples > > a <- xmlNode("arg", attrs = c(default="T"), + xmlNode("name", "foo"), xmlNode("defaultValue","1:10")) > > > > cleanEx(); ..nameEx <- "xmlOutput" > > ### * xmlOutput > > flush(stderr()); flush(stdout()) > > ### Name: xmlOutputBuffer > ### Title: XML output streams > ### Aliases: xmlOutputBuffer xmlOutputDOM > ### Keywords: file IO > > ### ** Examples > > con <- xmlOutputDOM() > con$addTag("author", "Duncan Temple Lang") > con$addTag("address", close=FALSE) > con$addTag("office", "2C-259") > con$addTag("street", "Mountain Avenue.") > con$addTag("phone", close=FALSE) > con$addTag("area", "908", attrs=c(state="NJ")) > con$addTag("number", "582-3217") > con$closeTag() # phone > con$closeTag() # address > > con$addTag("section", close=FALSE) > con$addNode(xmlTextNode("This is some text ")) > con$addTag("a","and a link", attrs=c(href="http://www.omegahat.org")) > con$addNode(xmlTextNode("and some follow up text")) > > con$addTag("subsection", close=FALSE) > con$addNode(xmlTextNode("some addtional text ")) > con$addTag("a", attrs=c(href="http://www.omegahat.org"), close=FALSE) > con$addNode(xmlTextNode("the content of the link")) > con$closeTag() # a > con$closeTag() # "subsection" > con$closeTag() # section > > d <- xmlOutputDOM() > d$addPI("S", "plot(1:10)") > d$addCData('x <- list(1, a="&");\nx[[2]]') > print(d$value()) > > > > cleanEx(); ..nameEx <- "xmlParent" > > ### * xmlParent > > flush(stderr()); flush(stdout()) > > ### Name: xmlParent > ### Title: Get parent node of XMLInternalNode > ### Aliases: xmlParent xmlParent.XMLInternalNode > ### Keywords: file IO > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "xmlRoot" > > ### * xmlRoot > > flush(stderr()); flush(stdout()) > > ### Name: xmlRoot > ### Title: Get the top-level XML node. > ### Aliases: xmlRoot xmlRoot.XMLDocument xmlRoot.XMLInternalDocument > ### xmlRoot.XMLDocumentRoot xmlRoot.XMLDocumentContent > ### xmlRoot.HTMLDocument > ### Keywords: file > > ### ** Examples > > doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML")) > xmlRoot(doc) mpg cyl disp hp drat wt qsec vs am gear carb 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 > > > > cleanEx(); ..nameEx <- "xmlSize" > > ### * xmlSize > > flush(stderr()); flush(stdout()) > > ### Name: xmlSize > ### Title: The number of sub-elements within an XML node. > ### Aliases: xmlSize xmlSize.default xmlSize.XMLDocument xmlSize.XMLNode > ### Keywords: file > > ### ** Examples > > fileName <- system.file("exampleData", "mtcars.xml", package="XML") > doc <- xmlTreeParse(fileName) > xmlSize(doc) [1] 2 > xmlSize(doc$doc$children[["dataset"]][["variables"]]) [1] 11 > > > > cleanEx(); ..nameEx <- "xmlSubset" > > ### * xmlSubset > > flush(stderr()); flush(stdout()) > > ### Name: [.XMLNode > ### Title: Convenience accessors for the children of XMLNode objects. > ### Aliases: [.XMLNode [[.XMLNode [[.XMLDocumentContent > ### Keywords: IO file > > ### ** Examples > > > > > > cleanEx(); ..nameEx <- "xmlTree" > > ### * xmlTree > > flush(stderr()); flush(stdout()) > > ### Name: xmlTree > ### Title: An internal, updatable DOM object for building XML trees > ### Aliases: xmlTree > ### Keywords: IO > > ### ** Examples > > tr <- xmlTree("Duncan") > tr$addTag("name", attrs=c(a=1,b="xyz"), close=FALSE) attr(,"class") [1] "XMLInternalNode" > tr$addTag("first", "Larry") attr(,"class") [1] "XMLInternalNode" > tr$addTag("last", "User") attr(,"class") [1] "XMLInternalNode" > tr$closeTag() attr(,"class") [1] "XMLInternalNode" > tr$value() attr(,"class") [1] "XMLInternalDocument" > cat(saveXML(tr$value())) Larry User > > tr <- xmlTree("CDataTest") > tr$addTag("top", close=FALSE) attr(,"class") [1] "XMLInternalNode" > tr$addCData("x <- list(1, a='&');\nx[[2]]") attr(,"class") [1] "XMLInternalNode" > tr$addPI("S", "plot(1:10)") attr(,"class") [1] "XMLInternalNode" > tr$closeTag() attr(,"class") [1] "XMLInternalNode" > cat(saveXML(tr$value())) > > > > cleanEx(); ..nameEx <- "xmlTreeParse" > > ### * xmlTreeParse > > flush(stderr()); flush(stdout()) > > ### Name: xmlTreeParse > ### Title: XML Parser > ### Aliases: xmlTreeParse htmlTreeParse > ### Keywords: file IO > > ### ** Examples > > fileName <- system.file("exampleData", "test.xml", package="XML") > # parse the document and return it in its standard format. > xmlTreeParse(fileName) $doc $file [1] "/CRANPkg/check/XML.Rcheck/XML/exampleData/test.xml" $version [1] "1.0" $children $children$comment $children$foo %testEnt; %extEnt; attr(,"class") [1] "XMLDocumentContent" $dtd $external NULL $internal $elements $elements$x $elements$y $entities $entities$testEnt $entities$logo attr(,"class") [1] "InternalDTD" attr(,"class") [1] "DTDList" attr(,"class") [1] "XMLDocument" > > # parse the document, discarding comments. > > xmlTreeParse(fileName, handlers=list("comment"=function(x,...){NULL}), asTree = TRUE) $doc $file [1] "/CRANPkg/check/XML.Rcheck/XML/exampleData/test.xml" $version [1] "1.0" $children $children$foo %testEnt; %extEnt; attr(,"class") [1] "XMLDocumentContent" $dtd $external NULL $internal $elements $elements$x $elements$y $entities $entities$testEnt $entities$logo attr(,"class") [1] "InternalDTD" attr(,"class") [1] "DTDList" attr(,"class") [1] "XMLDocument" > > # print the entities > invisible(xmlTreeParse(fileName, + handlers=list(entity=function(x) { + cat("In entity",x$name, x$value,"\n") + x} + ), asTree = TRUE + ) + ) > > # Parse some XML text. > # Read the text from the file > xmlText <- paste(readLines(fileName), "\n", collapse="") > > xmlTreeParse(xmlText, asText=TRUE) $doc $file [1] "" $version [1] "1.0" $children $children$comment $children$foo %testEnt; %extEnt; attr(,"class") [1] "XMLDocumentContent" $dtd $external NULL $internal $elements $elements$x $elements$y $entities $entities$testEnt $entities$logo attr(,"class") [1] "InternalDTD" attr(,"class") [1] "DTDList" attr(,"class") [1] "XMLDocument" > > # Read a MathML document and convert each node > # so that the primary class is > # MathML > # so that we can use method dispatching when processing > # it rather than conditional statements on the tag name. > # See plotMathML() in examples/. > fileName <- system.file("exampleData", "mathml.xml",package="XML") > m <- xmlTreeParse(fileName, + handlers=list( + startElement = function(node){ + cname <- paste(xmlName(node),"MathML", sep="",collapse="") + class(node) <- c(cname, class(node)); + node + })) > > > # In this example, we extract _just_ the names of the > # variables in the mtcars.xml file. > # The names are the contents of the > # tags. We discard all other tags by returning NULL > # from the startElement handler. > # > # We cumulate the names of variables in a character > # vector named `vars'. > # We define this within a closure and define the > # variable function within that closure so that it > # will be invoked when the parser encounters a > # tag. > # This is called with 2 arguments: the XMLNode object (containing > # its children) and the list of attributes. > # We get the variable name via call to xmlValue(). > > # Note that we define the closure function in the call and then > # create an instance of it by calling it directly as > # (function() {...})() > > # Note that we can get the names by parsing > # in the usual manner and the entire document and then executing > # xmlSApply(xmlRoot(doc)[[1]], function(x) xmlValue(x[[1]])) > # which is simpler but is more costly in terms of memory. > fileName <- system.file("exampleData", "mtcars.xml", package="XML") > doc <- xmlTreeParse(fileName, handlers = (function() { + vars <- character(0) ; + list(variable=function(x, attrs) { + vars <<- c(vars, xmlValue(x[[1]])); + NULL}, + startElement=function(x,attr){ + NULL + }, + names = function() { + vars + } + ) + })() + ) > > # Here we just print the variable names to the console > # with a special handler. > doc <- xmlTreeParse(fileName, handlers = list( + variable=function(x, attrs) { + print(xmlValue(x[[1]])); TRUE + }), asTree=TRUE) [1] "mpg" [1] "cyl" [1] "disp" [1] "hp" [1] "drat" [1] "wt" [1] "qsec" [1] "vs" [1] "am" [1] "gear" [1] "carb" /CRANPkg/check/XML.Rcheck/XML/exampleData/TestInvalid.xml:10: element title: validity error : Element title was declared EMPTY this one has content ^ /CRANPkg/check/XML.Rcheck/XML/exampleData/TestInvalid.xml:11: element doc: validity error : Element doc content does not follow the DTD, expecting (title , version), got (title ) ^ > > # This should raise an error. > try(xmlTreeParse( + system.file("exampleData", "TestInvalid.xml", package="XML"), + validate=TRUE)) Warning: Element doc content does not follow the DTD, expecting (title , version), got (title ) Warning: Element title was declared EMPTY this one has content Error in xmlTreeParse(system.file("exampleData", "TestInvalid.xml", package = "XML"), : XML document is invalid > > ## Not run: > ##D # Parse an XML document directly from a URL. > ##D # Requires Internet access. > ##D xmlTreeParse("http://www.omegahat.org/Scripts/Data/mtcars.xml", asText=TRUE) > ## End(Not run) > > counter = function() { + counts = integer(0) + list(startElement = function(node) { + name = xmlName(node) + if(name %in% names(counts)) + counts[name] <<- counts[name] + 1 + else + counts[name] <<- 1 + }, + counts = function() counts) + } > > h = counter() > xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML"), handlers = h, useInternalNodes = TRUE) $startElement function (node) { name = xmlName(node) if (name %in% names(counts)) counts[name] <<- counts[name] + 1 else counts[name] <<- 1 } $counts function () counts > h$counts() variable variables record dataset 11 1 32 1 > > > > getLinks = function() { + links = character() + list(a = function(node, ...) { + links <<- c(links, xmlGetAttr(node, "href")) + node + }, + links = function()links) + } > > h1 = getLinks() > htmlTreeParse(system.file("examples", "index.html", package = "XML"), handlers = h1) Warning: XML Parsing Error: /CRANPkg/check/XML.Rcheck/XML/examples/index.html:8: Unexpected end tag : font Warning: XML Parsing Error: /CRANPkg/check/XML.Rcheck/XML/examples/index.html:9: Unexpected end tag : font $a function (node, ...) { links <<- c(links, xmlGetAttr(node, "href")) node } $links function () links > h1$links() [1] "XML_0.97-0.tar.gz" "XML_0.97-0.zip" [3] "XML_0.97-0.tar.gz" "XML_0.97-0.zip" [5] "Overview.html" "manual.pdf" [7] "Tour.pdf" "description.pdf" [9] "WritingXML.html" "FAQ.html" [11] "Changes" "http://cm.bell-labs.com/stat/duncan" [13] "mailto:duncan@wald.ucdavis.edu" > > h2 = getLinks() > htmlTreeParse(system.file("examples", "index.html", package = "XML"), handlers = h2, useInternalNodes = TRUE) Warning: XML Parsing Error: /CRANPkg/check/XML.Rcheck/XML/examples/index.html:8: Unexpected end tag : font Warning: XML Parsing Error: /CRANPkg/check/XML.Rcheck/XML/examples/index.html:9: Unexpected end tag : font $a function (node, ...) { links <<- c(links, xmlGetAttr(node, "href")) node } $links function () links > all(h1$links() == h2$links()) [1] TRUE > > > > cleanEx(); ..nameEx <- "xmlValue" > > ### * xmlValue > > flush(stderr()); flush(stdout()) > > ### Name: xmlValue > ### Title: Extract the contents of a leaf XML node > ### Aliases: xmlValue xmlValue.XMLCDataNode xmlValue.XMLNode > ### xmlValue.XMLProcessingInstruction xmlValue.XMLTextNode > ### xmlValue.XMLComment > ### Keywords: file > > ### ** Examples > > node <- xmlNode("foo", "Some text") > xmlValue(node) [1] "Some text" > > xmlValue(xmlTextNode("some more raw text")) [1] "some more raw text" > > > > ### *