xmlToDataFrame: Extract data from a simple XML document

Description Usage Arguments Value Author(s) See Also Examples

Description

This function can be used to extract data from an XML document (or sub-document) that has a simple, shallow structure that does appear reasonably commonly. The idea is that there is a collection of nodes which have the same fields (or a subset of common fields) which contain primitive values, i.e. numbers, strings, etc. Each node corresponds to an "observation" and each of its sub-elements correspond to a variable. This function then builds the corresponding data frame, using the union of the variables in the different observation nodes. This can handle the case where the nodes do not all have all of the variables.

Usage

1
2
3
xmlToDataFrame(doc, colClasses = NULL, homogeneous = NA,
               collectNames = TRUE, nodes = list(),
               stringsAsFactors = default.stringsAsFactors())

Arguments

doc

the XML content. This can be the name of a file containing the XML, the parsed XML document. If one wants to work on a subset of nodes, specify these via the nodes parameter.

colClasses

a list/vector giving the names of the R types for the corresponding variables and this is used to coerce the resulting column in the data frame to this type. These can be named. This is similar to the colClasses parameter for read.table. If this is given as a list, columns in the data frame corresponding to elements that are NULL are omitted from the answer. This can be slightly complex to specify if the different nodes have the "variables" in quite different order as there is not a well defined order for the variables corresponding to colClasses.

homogeneous

a logical value that indicates whether each of the nodes contains all of the variables (TRUE) or if there may be some nodes which have only a subset of them. The function determines this if the caller does not specify homogeneous or uses NA as the value. It is a parameter to allow the caller to specify this information and avoid these "extra" computations. If the caller knows this information it is more efficient to specify it.

collectNames

a logical value indicating whether we compute the names by explicitly computing the union of all variable names or, if FALSE, we use the names from the node with the most children. This latter case is useful when the caller knows that the there is at least one node with all the variables.

nodes

a list of XML nodes which are to be processed

stringsAsFactors

a logical value that controls whether character vectors are converted to factor objects in the resulting data frame.

Value

A data frame.

Author(s)

Duncan Temple Lang

See Also

xmlParse getNodeSet

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 f = system.file("exampleData", "size.xml", package = "XML")
 xmlToDataFrame(f, c("integer", "integer", "numeric"))

   # Drop the middle variable.
 z = xmlToDataFrame(f, colClasses = list("integer", NULL, "numeric"))


   #  This illustrates how we can get a subset of nodes and process
   #  those as the "data nodes", ignoring the others.
  f = system.file("exampleData", "tides.xml", package = "XML")
  doc = xmlParse(f)
  xmlToDataFrame(nodes = xmlChildren(xmlRoot(doc)[["data"]]))

    # or, alternatively
  xmlToDataFrame(nodes = getNodeSet(doc, "//data/item"))


  f = system.file("exampleData", "kiva_lender.xml", package = "XML")
  doc = xmlParse(f)
  dd = xmlToDataFrame(getNodeSet(doc, "//lender"))

Example output

  age sex number
1   0   0    500
2   0   1    300
3   1   0    200
4   1   1    400
5  10   0     NA
        date day     time pred highlow
1 2010/11/13 Sat 06:08 AM 4.74       H
2 2010/11/13 Sat 11:52 AM 2.75       L
3 2010/11/13 Sat 04:50 PM 4.28       H
4 2010/11/13 Sat 11:36 PM 0.83       L
5 2010/11/14 Sun 06:49 AM 4.89       H
6 2010/11/14 Sun 12:56 PM  2.3       L
7 2010/11/14 Sun 06:07 PM 4.04       H
        date day     time pred highlow
1 2010/11/13 Sat 06:08 AM 4.74       H
2 2010/11/13 Sat 11:52 AM 2.75       L
3 2010/11/13 Sat 04:50 PM 4.28       H
4 2010/11/13 Sat 11:36 PM 0.83       L
5 2010/11/14 Sun 06:49 AM 4.89       H
6 2010/11/14 Sun 12:56 PM  2.3       L
7 2010/11/14 Sun 06:07 PM 4.04       H

XML documentation built on July 16, 2018, 1:01 a.m.

Related to xmlToDataFrame in XML...