Skip to contents

This function takes a file with XML input and processes it to generate corresponding output in CSV format. If the input is a character string, the output will be printed to the console. Otherwise, a CSV file will be generated with the same name as the input file but with a .csv extension.

Usage

xml_to_csv(file, n_tokens_limit = 2000, ...)

Arguments

file

A character string representing the path to the file with XML input.

n_tokens_limit

An integer representing the maximum number of tokens allowed in the input text. Defaults to 2000.

...

Additional arguments passed down to lower-level functions.

Value

If the input is a character string, the function returns the output as a character string and prints it to the console. Otherwise, the function returns the output as a data frame and generates a CSV file with the same name as the input file but with a .csv extension.

Examples

if (FALSE) {
# Example 1: XML string
xml_string <- '<?xml version="1.0"?>
<employees>
 <employee>
   <firstName>John</firstName>
   <lastName>Doe</lastName>
 </employee>
 <employee>
   <firstName>Anna</firstName>
   <lastName>Smith</lastName>
 </employee>
 <employee>
   <firstName>Peter</firstName>
   <lastName>Jones</lastName>
 </employee>
</employees>'
xml_to_csv(xml_string)

# Example 2: XML file
# create temporary file
temp_file <- tempfile(fileext = ".xml")

# write XML content to temporary file
cat('<?xml version="1.0"?>
<employees>
  <employee>
    <firstName>John</firstName>
    <lastName>Doe</lastName>
  </employee>
  <employee>
    <firstName>Anna</firstName>
    <lastName>Smith</lastName>
  </employee>
  <employee>
    <firstName>Peter</firstName>
    <lastName>Jones</lastName>
  </employee>
</employees>', file = temp_file)

# call xml_to_csv function with temporary file
xml_to_csv(temp_file)

# remove temporary file
file.remove(temp_file)
}