Learn R Programming

XML (version 3.99-0.17)

makeClassTemplate: Create S4 class definition based on XML node(s)

Description

This function is used to create an S4 class definition by examining an XML node and mapping the sub-elements to S4 classes. This works very simply with child nodes being mapped to other S4 classes that are defined recursively in the same manner. Simple text elements are mapped to a generic character string. Types can be mapped to more specific types (e.g. boolean, Date, integer) by the caller (via the types) parameter. The function also generates a coercion method from an XMLAbstractNode to an instance of this new class.

This function can either return the code that defines the class or it can define the new class in the R session.

Usage

makeClassTemplate(xnode, types = character(), default = "ANY",
                   className = xmlName(xnode), where = globalenv())

Value

A list with 4 elements:

name

the name of the new class

slots

a character vector giving the slot name and type name pairs

def

code for defining the class

coerce

code for defining the coercion method from an XMLAbstractNode to an instance of the new class

If where is not NULL, the class and coercion code is actually evaluated and the class and method will be defined in the R session as a side effect.

Arguments

xnode

the XML node to analyze

types

a character vector mapping XML elements to R classes

default

the default class to map an element to

className

the name of the new top-level class to be defined. This is the name of the XML node (without the name space)

where

typically either an environment or NULL. This is used to control where the class and coercion method are defined or if NULL inhibits the code being evaluated. In this case, the code is returned as strings.

Author

Duncan Temple Lang

See Also

xmlToS4

Examples

Run this code
txt = paste0("ABCXYZ",
            "3.54available")
 doc = xmlParse(txt)

 code = makeClassTemplate(xmlRoot(doc)[[1]], types = c(cost = "numeric"))

 as(xmlRoot(doc)[["part"]], "part")

Run the code above in your browser using DataLab