Create Character Vectors from an XML File

DESCRIPTION:

Parse an XML file following the specified XML paths and creating appropriate character vectors.

USAGE:

parseXMLPathFile(file, path, default=character(0), delimiters=character(0), 
        separateDelimiters=F)

REQUIRED ARGUMENTS:

file
a character string containing the name of the XML file to parse.
path
a vector or list of character strings containing one or more XML paths.

OPTIONAL ARGUMENTS:

default
a character string providing a default return value for invalid paths.
delimiters
a character string specifying the delimiter to use when parsing.
separateDelimiters
a logical value. If T, successive delimiters will create an empty value.

VALUE:

a named list of character vectors with one list element for each string in the argument path, where the names of the list elements are the path strings. Each list element contains the parsed strings corresponding to a XML path string.

REFERENCES:

http://www.w3.org/XML

SEE ALSO:

, , , , .

EXAMPLES:

# Create an XML file and parse for given XML paths
xmlfile <- file.path(path.expand("~"), "bookex.xml")
cat(paste("<library>",
             "<book id=\"web1\">",
                 "<title>Dictionary</title>",
             "</book>",
             "<book id=\"web2\">",
                 "<title>Spanish 2 English</title>",
             "</book>",
             "<book id=\"1\">",
                 "<title>Encyclopedia - A</title>",
             "</book>",
             "<book id=\"2\">",
                 "<title>Encyclopedia - B</title>",
             "</book>",
           "</library>", sep=" "),
    file=xmlfile)
xpaths <- c("library/book/@id",
           "library/book/title/text()",
           "library/book[@id = 'web1']/title/text()",
           "library/book[@id < 2]/title/text()")
parseXMLPathFile(xmlfile, xpaths)

# Create an XML file for an S-PLUS data frame and parse with an XML path
xmlfile <- file.path(path.expand("~"), "fuelframe.xml")
createXMLFile(fuel.frame, xmlfile)
parseXMLPathFile(xmlfile, "S-PLUS/DataFrame/Columns/Column[@name = 'Fuel']/Items/Item/text()")

# Create an XML file for an S-PLUS vector using a delimiter and parse
xmlfile <- file.path(path.expand("~"), "vector.xml")
x <- 1:5
annotation <- "An example of XML file with a delimiter" 
del=","
createXMLFile(x, xmlfile, delimiter=del)
# get "1,2,3,4,5"
parseXMLPathFile(xmlfile, "S-PLUS/Vector/Items/text()")
# get an int array: [1] 1" "2" "3" "4" "5"
parseXMLPathFile(xmlfile, "S-PLUS/Vector/Items/text()", delimiters=del)