Learn R Programming

XML (version 3.98-1.5)

xmlNode: Create an XML node

Description

These functions allow one to create XML nodes as are created in C code when reading XML documents. Trees of XML nodes can be constructed and integrated with other trees generated manually or with via the parser.

Usage

xmlNode(name, ..., attrs=NULL, namespace="", namespaceDefinitions = NULL, .children = list(...)) xmlTextNode(value, namespace="", entities = XMLEntities, cdata = FALSE) xmlPINode(sys, value, namespace="") xmlCDataNode(...) xmlCommentNode(text)

Arguments

name
The tag or element name of the XML node. This is what appears in the elements as ..
...
The children nodes of this XML node. These can be objects of class XMLNode or arbitrary values that will be converted to a string to form an XMLTextNode object.
.children
an alternative mechanism to specifying the children which is useful for programmatic use when one has the children in an existing list. The ... mechanism is for use when the children are specified directly and individually.
attrs
A named character vector giving the name, value pairs of attributes for this XML node.
value
This is the text that is to be used when forming an XMLTextNode.
cdata
a logical value which controls whether the text being used for the child node is to be first enclosed within a CDATA node to escape special characters such as > and &.
namespace
The XML namespace identifier for this node.
namespaceDefinitions
a collection of name space definitions, containing the prefixes and the corresponding URIs. This is most conveniently specified as a character vector whose names attribute is the vector of prefixes and whose values are the URIs. Alternatively, one can provide a list of name space definition objects such as those returned
sys
the name of the system for which the processing instruction is targeted. This is the value that appears in the
text
character string giving the contents of the comment.
entities
a character vector giving the mapping from special characters to their entity equivalent. This provides the character-expanded entity pairings of 'character = entity' , e.g. '

Value

An object of class XMLNode. In the case of xmlTextNode, this also inherits from XMLTextNode. The fields or slots that objects of these classes have include name, attributes, children and namespace. However, one should the accessor functions xmlName, xmlAttrs, xmlChildren and xmlNamespace

References

http://www.w3.org/XML, http://www.jclark.com/xml, http://www.omegahat.net

See Also

addChildren xmlTreeParse asXMLNode newXMLNode newXMLPINode newXMLCDataNode newXMLCommentNode

Examples

Run this code

 # node named arg with two children: name and defaultValue
 # Both of these have a text node as their child.
 n <- xmlNode("arg", attrs = c(default="TRUE"),
               xmlNode("name", "foo"), xmlNode("defaultValue","1:10"))


  # internal C-level node.
 a = newXMLNode("arg", attrs = c(default = "TRUE"),
                newXMLNode("name", "foo"),
                newXMLNode("defaultValue", "1:10"))

 xmlAttrs(a) = c(a = 1, b = "a string")

 xmlAttrs(a) = c(a = 1, b = "a string", append = FALSE)

 newXMLNamespace(a, c("r" = "http://www.r-project.org"))
 xmlAttrs(a) = c("r:class" = "character") 


 xmlAttrs(a[[1]]) = c("r:class" = "character") 


  # Using a character vector as a namespace definitions
 x = xmlNode("bob",
             namespaceDefinitions = c(r = "http://www.r-project.org",
                                      omg = "http://www.omegahat.net"))

Run the code above in your browser using DataLab