MyNixOS website logo
Description

Comfortably Converting XML Documents to Dataframes and Vice Versa.

Converts XML documents to R dataframes and dataframes to XML documents. A wide variety of options allows for different XML formats and flexible control of the conversion process. Results can be exported to CSV and Excel, if desired. Also converts XML data to R lists.

Package xmlconvert

xmlconvert logo

What xmlconvert does

The xmlconvert package is made to easily and comfortably convert XML data to R dataframes and the other way around. It provides a lot of options to control the conversion process and to export the results to CSV or Excel files, if desired.

How you install xmlconvert

Just execute install.packages("xmlconvert", dependencies = TRUE) in the R console to install the package including all packages it depends on. By default, the xlsx package for writing Excel files is not installed together with xmlconvert. xmlconvert will tell you when you need xlsx and will ask you to install it then.

How you work with xmlconvert

The functions xml_to_df() and df_to_xml() are the two workhorses of the package.

The functions assume that each data record (e.g. a customer) in the XML data is stored in one XML element/tag; the individual fields (e.g. name, address, e-mail), however, can be represented in different ways.

An XML data file could like this:

<datarecord>
  <field1>value1-1</field1>
  <field2>value1-2</field2>
</datarecord>

<datarecord>
  <field1>value2-1</field1>
  <field2>value2-2</field2>
</datarecord>

When working with the xml_to_df() and df_to_xml() functions, we would first of all use the records.tags argument to provide the tag name of the XML element that represents the records, in this example datarecord. Alternatively, the xml_to_df()’s records.xpath argument can be used to supply an XPath expression describing the location of the data records.

In the next step, we need to specify how to find the fields. In the example above, each field within a data record has its own XML element, and the tag name of this element is the field name. In this case, we would use fields = "tags" to make it clear to the xmlconvert functions that the fields are represented by XML elements. Not always will the tag names be the field names. The XML could also look like this:

<datarecord>
  <field name="field1">value1-1</field>
  <field name="field2">value1-2</field>
</datarecord>

<datarecord>
  <field name="field1">value2-1</field>
  <field name="field2">value2-2</field>
</datarecord>

Here, the XML elements representing the fields all have the tag field. The name of the field is not given by the tag name but is an attribute (called name in our case) of the field element. This attribute name can be supplied to the xmlconvert functions using the fields.names argument.

In this case, xml_to_df() could be called like this: xml_to_df("mydata.xml", records.tag = "datarecord", fields = "tags", fields.names = "name")

However, instead of being XML elements, the data fields could also be represented by individual attributes, as in the following example:

<datarecord field1="value1-1" field2="value1-2" />
<datarecord field1="value2-1" field2="value2-2" />

In this case, we use fields = "attributes" instead of fields = "tags" to adjust to this XML data structure. Here, a call of xml_to_df() could look like this: xml_to_df("mydata.xml", records.tag = "datarecord", fields = "attributes")

The xmlconvert functions offer a lot of options to specify which data fields are to be used, how to deal with missing values, how to treat different data types and how to export the results. Please consult the online help by executing ?xml_to_df in the R console for more information on these options.

Contact the author

Joachim Zuckarelli

Twitter: [@jsugarelli](https://twitter.com/jsugarelli)

GitHub: https://github.com/jsugarelli/xmlconvert.

Metadata

Version

0.1.2

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows