Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ xmlr Public

❗ This is a read-only mirror of the CRAN R package repository. xmlr — Read, Write and Work with 'XML' Data. Homepage: https://github.com/Alipsa/xmlr Report bugs for this package: https://github.com/Alipsa/xmlr/issues

License

Notifications You must be signed in to change notification settings

cran/xmlr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>
> 

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.

About

❗ This is a read-only mirror of the CRAN R package repository. xmlr — Read, Write and Work with 'XML' Data. Homepage: https://github.com/Alipsa/xmlr Report bugs for this package: https://github.com/Alipsa/xmlr/issues

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages