Help with the preparation of xml files
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

lifecycle Travis build status AppVeyor build status Coverage status

Package {xmlprocessor}

The goal of xmlprocessor is to help with the preparation of xml files. It was motivated by the laborious work of xml files processing that are used for simulations in an agronomic model (STICS). It strongly rests on the {xml2} package but contains functions that are wrappers for the creation and filling of xml template.

Installation

You can install the development version of xmlprocessor from GitHub with:

# install.packages("devtools")
devtools::install_github("BenjaminLouis/xmlprocessor")

Use package {xmlprocessor}

library(xmlprocessor)

Fill a xml template

One of the main goal of package {xmlprocessor} is to facilitate the serial creation of xml files. From a xml template, you can use a dataframe to replace text and attribute values.

Starting with the creation of a xml template :

library(xml2)
template <- read_xml("<parent>
                        <child nom='child1'>
                          <grandchild>grandchild1</grandchild>
                          <grandchild>grandchild2</grandchild>
                        </child>
                        <child nom='child2'>
                          null
                        </child>
                      </parent>")

The xml template contains 4 values that can be changed : grandchild1, grandchild2, child1 and child2. The first two are texts and the two latter are attributes values.

To fill the template with different values, you can build a dataframe where the columns names are the exact text to be replaced. For text values, this should be the whole text between xml element (<...>thistext</...>) and for attributes values, this should be the whole text between quotes.

toreplace <- data.frame(child1 = c("Ben", "Thomas"), child2 = c("Sarah", "Marie"),
grandchild1 = c("Brad", "Angelica"), grandchild2 = c("Jude", "Lea"))

You can then fill the template with information contained in toreplace to create a list with two xml documents with the structure as template but where grandchild1, grandchild2, child1 and child2 are replaced by the corresponding values in toreplace.

# No export, no collapsing
newxml <- xml_fill_template(xmltemplate = template, with = toreplace)
# Export and collapsing
newxml <- xml_fill_template(template, toreplace, collapse = "grandparent",
export = getwd(), name = "testexport")

Vignette

This package also contains a vignette that explained with more details the use of package {xmlprocessor}. After installing it, you can see the vignette through :

vignette("use_of_xmlprocessor", package = "xmlprocessor")

Code of conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.