Manipulating XML and JSON data

Mahbubul Majumder, PhD
Oct 16, 2014

What is XML?

  • It means EXtensible Markup Language

    • uses tags like HTML
    • but not HTML tags, instead user defined tags
  • Unlike HTML, it is designed to describe data

  • Data are self explained in XML format

    • software independent
    • hardware independent
    • very useful for transforming data in various platform
    • not too difficult to learn
  • Data becomes verbose

    • for good reason though

How HTML works with XML?

  • XML separates data from HTML codes, so one can concentrate on

    • HTML style or CSS to effectively display data
    • XML data are stored in a different place and HTML can easily display
  • Both works as a complement of each other

    • HTML helps display data
    • XML makes data easily transferable to any platform
    • XML also uses tags and attributes to describe data
  • Some data structures can be very messy, but with XML we can control some

    • The usual table form just can't do it
  • Scalable Vector Graphics (SVG) are written in XML

    • some of the figures you see on the web are not pictures
    • they are SVG and extremely scalable

XML data tree



  • Top node is root and bottom colored nodes are leaves

  • Each node is an XML tag

  • Each leaf is data value

xml-tree

Displaying JSON data using JavaScript

<html>
<body>

<h2>Display JSON data</h2>

<p id="p1"></p>

<script>
var myData = '{"name":"Mahbubul Majumder","dept":"Dept. of Mathematics","phone":"402 5542734"}'

var obj = JSON.parse(myData);

document.getElementById("p1").
  innerHTML = obj.name + "<br>" + 
  obj.dept + "<br>" +
  obj.phone;
</script>

</body>
</html>

Display JSON data

Mahbubul Majumder
Dept. of Mathematics
402 5542734

Getting JSON data into R

  • The package rjson is created for this
# install.packages("rjson")
library(rjson)
jData <- toJSON(head(women))
jData
[1] "{\"height\":[58,59,60,61,62,63],\"weight\":[115,117,120,123,126,129]}"
fromJSON(jData)
$height
[1] 58 59 60 61 62 63

$weight
[1] 115 117 120 123 126 129

Reading assignment and references