extrobe
2/12/2017 - 10:06 AM

Read XML Files

Read XML Files

<INVENTORY>
  <ITEM>
  <ITEMID>2357</ITEMID>
  <ITEMTYPE>P</ITEMTYPE>
  <COLOR>11</COLOR>
  <MINQTY>9</MINQTY>
  </ITEM>
  <ITEM>
  <ITEMID>2420</ITEMID>
  <ITEMTYPE>P</ITEMTYPE>
  <COLOR>11</COLOR>
  <MINQTY>3</MINQTY>
  </ITEM>
  <ITEM>
  <ITEMID>2431</ITEMID>
  <ITEMTYPE>P</ITEMTYPE>
  <COLOR>11</COLOR>
  <MINQTY>8</MINQTY>
  </ITEM>
  <ITEM>
  <ITEMID>2555</ITEMID>
  <ITEMTYPE>P</ITEMTYPE>
  <COLOR>11</COLOR>
  <MINQTY>30</MINQTY>
  </ITEM>
</INVENTORY>
# This script extracts values from an XML file and converts it to a dataframe
# Sample data to test with provided in a separate file - copy it to clipboard to use.

require(XML)

#Read the data from clipboard
data<-readClipboard()

#Convert to a dataframe
xmldataframe <- xmlToDataFrame(data)

#convert the qty field from Factor to Integer
xmldataframe$qty <- as.integer(xmldataframe$MINQTY)

#Sum the result
sum(xmldataframe$qty)