
Last chance! 50% off unlimited learning
Sale ends in
xmlTree
as the functions need to be used within a strict regime to avoid
corrupting C level structures.xmlDoc
creates a new XMLInternalDocument
object by copying the given node and all of its
descendants and putting them into a new document.
This is useful when we want to work with sub-trees
with general tools that work on documents, e.g. XPath queries.
newXMLDoc
allows one to create a regular XML node
with a name and attributes.
One can provide new namespace definitions via
namespaceDefinitions
. While these might also
be given in the attributes in the slightly more verbose
form of c('xmlns:prefix' = 'http://...')
,
the result is that the XML node does not interpret that
as a namespace definition but merely an attribute with
a name 'xmlns:prefix'.
Instead, one should specify the namespace definitions via
the namespaceDefinitions
parameter.
In addition to namespace definitions, a node name can also have a
namespace definition. This can be specified in the name
argument
as prefix:name
and newXMLDoc
will do the right thing in
separating this into the namespace and regular name. Alternatively, one
can specify a namespace separately via the namespace
argument.
This can be either a simple name or an internal namespace object defined
earlier.
How do we define a default namespace?
xmlDoc(node, addFinalizer = TRUE)
newXMLDoc(dtd = "", namespaces=NULL, addFinalizer = TRUE, name = character(), node = NULL, isHTML = FALSE)
newHTMLDoc(dtd = "loose", addFinalizer = TRUE, name = character(), node = newXMLNode("html", newXMLNode("head", addFinalizer = FALSE), newXMLNode("body", addFinalizer = FALSE), addFinalizer = FALSE))
newXMLNode(name, ..., attrs = NULL, namespace = character(), namespaceDefinitions = character(), doc = NULL, .children = list(...), parent = NULL,
at = NA, cdata = FALSE, suppressNamespaceWarning = getOption("suppressXMLNamespaceWarning", FALSE), sibling = NULL, addFinalizer = NA, noNamespace = length(namespace) == 0 && !missing(namespace), fixNamespaces = c(dummy = TRUE, default = TRUE))
newXMLTextNode(text, parent = NULL, doc = NULL, cdata = FALSE, escapeEntities = is(text, "AsIs"), addFinalizer = NA)
newXMLCDataNode(text, parent = NULL, doc = NULL, at = NA, sep = "\n", addFinalizer = NA)
newXMLCommentNode(text, parent = NULL, doc = NULL, at = NA, addFinalizer = NA)
newXMLPINode(name, text, parent = NULL, doc = NULL, at = NA, addFinalizer = NA)
newXMLDTDNode(nodeName, externalID = character(), systemID = character(), doc = NULL, addFinalizer = NA)
XMLInternalNode
object that will be copied to
create a subtree for a new document.c(shelp = "http://www.omegahat.net/XML/SHelp")
getNativeSymbolInfo
. xmlns:prefix='http:/...'
.
Instead, such definitions should be specified
ideally via the namespaceDefinitions
argument,
or even the namespace
argument.
The reason is that namespace definitions are special attributes
that are shared across nodes wherease regular attributes are
particular to a node. So a namespace needs to be explicitly defined
so that the XML representation can recognize it as such.
names
attribute
and c) whose value does not start with http:/
or
ftp:/
, then it is assumed that the value is a
namespace prefix for a namespace defined in an ancestor node.
To be able to resolve this prefix to a namespace definition,
parent
must be specified so that we can traverse the chain of ancestor nodes.
However, if c) does not hold, i.e. the string starts with http:/
or
ftp:/
,
then we take this single element to be a namespace definition and
the since it has no name b), this is the definition for the default namespace for this
new node, i.e. corresponding to xmlns='http:/...'
.
It is cumbersome to specify ""
as a name for an element in a
character vector (as c('' = 'value') gives an unnecessary
error!
.
Elements with names are expanded to namespace definitions
with the name as the prefix and the value as the namespace URI.
XMLInternalDocument
object created with
newXMLDoc
that is used to root the node.A//B//C//D
.
A is either + or -; B identifies the person or insitution that defined
the format (i.e. the "creator");
C is the name of the format; and language is an encoding for the
language that comes from the ISO 639 document.namespace
.
The values here are used only for defining new namespaces
and not for determining the namespace to use for this
particular node.
addChildren
.parent
)
this should be an XMLInternalNode
and the new node is added as
a sibling of this node, after this node, i.e. to the right.
This is just a convenient form of parent = xmlParent(node)
.TRUE
) or not (FALSE
). This is a
convenience mechanism to avoid having to create the text node and
then the CDATA node. If one is not certain what characters are in
the text, it is useful to use TRUE
to ensure that they are
“escaped”.
It is an argument for newXMLNode
as the child nodes can be
given as simple strings and are converted to text nodes. This
cdata
value is passed to the calls to create these text nodes
and so controls whether they are enclosed within CDATA nodes.
addChildren
saveXML
dummy
and default
. The dummy
element controls
whether we process child nodes that have a namespace which was not
defined when the node was created. These are created as “dummy”
namespaces and can be resolved now that the parent node is defined and
the name space may be defined. When we know it is not yet defined, but
will be defined in an ancestor node, we can turn off this processing
with a value of FALSE
. The default
element controls how we process the child nodes
and give them the default name space defined in the parent or ancestor nodes.
XMLInternalDocument
and XMLInternalNode
, respectively
xmlTree
saveXML
doc = newXMLDoc()
# Simple creation of an XML tree using these functions
top = newXMLNode("a")
newXMLNode("b", attrs = c(x = 1, y = 'abc'), parent = top)
newXMLNode("c", "With some text", parent = top)
d = newXMLNode("d", newXMLTextNode("With text as an explicit node"), parent = top)
newXMLCDataNode("x <- 1\n x > 2", parent = d)
newXMLPINode("R", "library(XML)", top)
newXMLCommentNode("This is a comment", parent = top)
o = newXMLNode("ol", parent = top)
kids = lapply(letters[1:3],
function(x)
newXMLNode("li", x))
addChildren(o, kids)
cat(saveXML(top))
x = newXMLNode("block", "xyz", attrs = c(id = "bob"),
namespace = "fo",
namespaceDefinitions = c("fo" = "http://www.fo.org"))
xmlName(x, TRUE) == "fo"
# a short cut to define a name space and make it the prefix for the
# node, thus avoiding repeating the prefix via the namespace argument.
x = newXMLNode("block", "xyz", attrs = c(id = "bob"),
namespace = c("fo" = "http://www.fo.org"))
# name space on the attribute
x = newXMLNode("block", attrs = c("fo:id" = "bob"),
namespaceDefinitions = c("fo" = "http://www.fo.org"))
x = summary(rnorm(1000))
d = xmlTree()
d$addNode("table", close = FALSE)
d$addNode("tr", .children = sapply(names(x), function(x) d$addNode("th", x)))
d$addNode("tr", .children = sapply(x, function(x) d$addNode("td", format(x))))
d$closeNode()
# Just doctype
z = xmlTree("people", dtd = "people")
# no public element
z = xmlTree("people", dtd = c("people", "", "http://www.omegahat.net/XML/types.dtd"))
# public and system
z = xmlTree("people", dtd = c("people", "//a//b//c//d", "http://www.omegahat.net/XML/types.dtd"))
# Using a DTD node directly.
dtd = newXMLDTDNode(c("people", "", "http://www.omegahat.net/XML/types.dtd"))
z = xmlTree("people", dtd = dtd)
x = rnorm(3)
z = xmlTree("r:data", namespaces = c(r = "http://www.r-project.org"))
z$addNode("numeric", attrs = c("r:length" = length(x)), close = FALSE)
lapply(x, function(v) z$addNode("el", x))
z$closeNode()
# should give <r:data><numeric r:length="3"/></r:data>
# shows namespace prefix on an attribute, and different from the one on the node.
z = xmlTree()
z$addNode("r:data",
namespace = c(r = "http://www.r-project.org",
omg = "http://www.omegahat.net"),
close = FALSE)
x = rnorm(3)
z$addNode("r:numeric", attrs = c("omg:length" = length(x)))
z = xmlTree("people", namespaces = list(r = "http://www.r-project.org"))
z$setNamespace("r")
z$addNode("person", attrs = c(id = "123"), close = FALSE)
z$addNode("firstname", "Duncan")
z$addNode("surname", "Temple Lang")
z$addNode("title", "Associate Professor")
z$addNode("expertize", close = FALSE)
z$addNode("topic", "Data Technologies")
z$addNode("topic", "Programming Language Design")
z$addNode("topic", "Parallel Computing")
z$addNode("topic", "Data Visualization")
z$closeTag()
z$addNode("address", "4210 Mathematical Sciences Building, UC Davis")
#
txt = newXMLTextNode("x < 1")
txt # okay
saveXML(txt) # x < 1
# By escaping the text, we ensure the entities don't
# get expanded, i.e. < doesn't become <
txt = newXMLTextNode(I("x < 1"))
txt # okay
saveXML(txt) # x < 1
newXMLNode("r:expr", newXMLTextNode(I("x < 1")),
namespaceDefinitions = c(r = "http://www.r-project.org"))
Run the code above in your browser using DataLab