xmlr

XML creating, manipulating, and reading package for Renjin

License

License

MIT
GroupId

GroupId

se.alipsa
ArtifactId

ArtifactId

xmlr
Last Version

Last Version

0.2.1
Release Date

Release Date

Type

Type

jar
Description

Description

xmlr
XML creating, manipulating, and reading package for Renjin
Project URL

Project URL

https://github.com/alipsa/xmlr
Source Code Management

Source Code Management

https://github.com/alipsa/xmlr/tree/master

Download xmlr

How to add to project

<!-- https://jarcasting.com/artifacts/se.alipsa/xmlr/ -->
<dependency>
    <groupId>se.alipsa</groupId>
    <artifactId>xmlr</artifactId>
    <version>0.2.1</version>
</dependency>
// https://jarcasting.com/artifacts/se.alipsa/xmlr/
implementation 'se.alipsa:xmlr:0.2.1'
// https://jarcasting.com/artifacts/se.alipsa/xmlr/
implementation ("se.alipsa:xmlr:0.2.1")
'se.alipsa:xmlr:jar:0.2.1'
<dependency org="se.alipsa" name="xmlr" rev="0.2.1">
  <artifact name="xmlr" type="jar" />
</dependency>
@Grapes(
@Grab(group='se.alipsa', module='xmlr', version='0.2.1')
)
libraryDependencies += "se.alipsa" % "xmlr" % "0.2.1"
[se.alipsa/xmlr "0.2.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.renjin.cran » testthat jar 2.1.1-b2

provided (3)

Group / Artifact Type Version
org.renjin » renjin-script-engine jar 3.5-beta76
org.slf4j : slf4j-api jar 1.7.27
org.slf4j : slf4j-log4j12 jar 1.7.27

Project Modules

There are no modules declared in this project.

xmlr

XML dom package for R implemented using Reference Classes

The jdom project (www.jdom.org) provided a lot of initial inspiration for the api but there are several differences, mainly

  • Attributes: Attribute is not a specific class but just a name value element of the named list property of an Element
  • Namespaces: Whereas in jdom namespaces is a special class that exists as a specific object attribute, xmlr takes a "simpler" approach in the sense that namespace declarations are just another element attribute and name space prefixes are part of the element name. This might change in the future but for now this is how it is done.

You can create and xmlr Document programmatically or by parsing text or a file.

Creating the DOM programmatically

To create the following xml

<table xmlns='http://www.w3.org/TR/html4/'>
    <tr>
        <td>Apples</td>
        <td>Bananas</td>
    </tr>
</table>

You could do something like this:

 doc <- Document$new()
  root <- Element$new("table")
  root$setAttribute("xmlns", "http://www.w3.org/TR/html4/")

  root$addContent(
    Element$new("tr")
      $addContent(Element$new("td")$setText("Apples"))
      $addContent(Element$new("td")$setText("Bananas"))
  )
  doc$setRootElement(root)

Or you could do like this:

doc2 <- parse.xmlstring("
<table xmlns='http://www.w3.org/TR/html4/'>
    <tr>
        <td>Apples</td>
        <td>Bananas</td>
    </tr>
</table>")

Note that there is no pretty print available (yet) so if you print it print(doc2) it will look like this:

> print(doc2)
<table xmlns='http://www.w3.org/TR/html4/'><tr><td>Apples</td><td>Bananas</td></tr></table>
> 

using xmlr

xmlr is published both to CRAN for use in GNU R and to Maven Central for use in Renjin. For cran it is as simple as install.packages('xmlr') followed by library('xmlr'). For renjin add the following to your pom.xml:

<dependency>
  <groupId>se.alipsa</groupId>
  <artifactId>xmlr</artifactId>
  <version>0.2.1</version>
</dependency>

...and then library('se.alipsa:xmlr')

Limitations

Processing instructions, custom entity references, and comments are not yet supported.

Any proper xml including CDATA, comments, processing instructions etc. can be parsed though, it is just that only elements, attributes and text will be retained.

CDATA can be read from string or file but handled as ordinary text after that. I.e. the output might not be valid XML.

There are probably issues (memory, performance) with very large XML trees.

Why xmlr?

I had problems on one of my machines to install the XML package (some gcc issue) so needed an alternative. As I have been thinking about doing something more comprehensive with Reference Classes I decided to create a pure base-R DOM model with some simple ways to do input and output to strings and files. As it turned out to be useful to me, I thought it might be for others as well, so I decided to open source and publish it.

se.alipsa

Alipsa

Versions

Version
0.2.1
0.2.0-SNAPHOT
0.2.0