Guest User

Untitled

a guest
Feb 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. //Function to parse data from xml
  2.  
  3. parse_xml <-function(FileName) {
  4. xmldata <- xmlParse(FileName)
  5. result_nodes = XML::getNodeSet(xmldata , "//IOTModellerLog/Event")
  6. if (!length(result_nodes)==0){
  7. abc <- rbindlist(lapply(result_nodes,function(x) data.frame(as.list(unlist(xmlToList(x))))),use.names = TRUE, fill = TRUE)
  8.  
  9. ....do other calculations....
  10.  
  11. abc <- data.frame(lapply(abc, as.character), stringsAsFactors = FALSE)
  12. }
  13. }
  14.  
  15. sbind = function(x, y, fill=NA) {
  16. sbind.fill = function(d, cols){
  17. for(c in cols)
  18. d[[c]] = fill
  19. d
  20. }
  21. x = sbind.fill(x, setdiff(names(y),names(x)))
  22. y = sbind.fill(y, setdiff(names(x),names(y)))
  23.  
  24. rbind(x, y)
  25. }
  26.  
  27. path <- "/home/mycomp/Documents/xmls/xmlfrommtar"
  28.  
  29. workerFunc <- function(file) {
  30. dat <- parse_xml(file)
  31. df <- sbind(df,dat)
  32. }
  33.  
  34. cl <- makeCluster(detectCores())
  35. files <- dir(path, pattern="*.xml|*.XML", full.names = T)
  36. part <- clusterSplit(cl, seq_along(files))
  37. filesPart <- lapply(part, function(p) files[p])
  38. results <- clusterApply(cl, filesPart, workerFunc)
  39.  
  40. Error in checkForRemoteErrors(val) :
  41. 4 nodes produced errors; first error: could not find function "parse_xml"
  42.  
  43. IOTModellerLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DeviceID="7430180" ClientID="12324" FileCreationDate="2017-03-01T22:40:03" FileVersion="2" EventClassID="65535" IOTLogCreationDate="2017-03-01T12:29:54" SampleID="1" xsi:noNamespaceSchemaLocation="/opt/nds/ams_proxy/webapps/ams_proxy/WEB-INF/amsXmlSchema.xsd">
  44. <Event EventTime="2017-02-27T18:33:58">
  45. <IOTEvent State="PowerOn" />
  46. </Event>
  47. <Event EventTime="2017-02-28T08:59:03">
  48. <DataEvent>
  49. <Model>1</Model>
  50. <DataType>1</DataType>
  51. <DataValue>0301</DataValue>
  52. </DataEvent>
  53. </Event>
  54. <Event EventTime="2017-02-28T08:59:13">
  55. <DataEvent>
  56. <Model>1</Model>
  57. <DataType>1</DataType>
  58. <DataValue>0401</DataValue>
  59. </DataEvent>
  60. </Event>
  61. </IOTModellerLog>
Add Comment
Please, Sign In to add comment