无法解析R中的xml文件

时间:2017-11-08 11:53:44

标签: r xml

这里是需要解析并最终在dataframe-

中转换的文件

<?xml version="1.0" encoding="UTF-8"?>

-<message hash="fb73481d3f3d2b9a70733d69268de71c84f151f8" type="xml" sessionid="https" connector_id="4510010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<comment/>


-<drive utc_timestamp="2015-09-03T00:09:01.362058" report_name="HTTP Logging to RSP" device_type="ACS 800" sw_appl="ASARF012" sw_type="AS7R7322" serial_number="-">


-<signals timezone="UTC +00:00">

<signal unit="rpm" scale="1" timestamp="2015-09-03T00:07:28.006" name="02_02_01_speed_ref_2" value="0"/>

<signal unit="%" scale="1" timestamp="2015-09-03T00:07:28.511" name="02_01_05_torque" value="0"/>

<signal unit="C" scale="1" timestamp="2015-09-03T00:07:45.352" name="02_01_32_pp_3_temp" value="0"/>

<signal unit="C" scale="1" timestamp="2015-09-03T00:07:46.203" name="02_01_33_pp_4_temp" value="0"/>

<signal unit="C" scale="1" timestamp="2015-09-03T00:07:48.155" name="02_01_35_motor_1_temp" value="0"/>

<signal unit="C" scale="1" timestamp="2015-09-03T00:07:48.911" name="02_01_36_motor_2_temp" value="0"/>

<signal unit="C" scale="1" timestamp="2015-09-03T00:07:52.723" name="02_01_37_motor_temp_est" value="29.999969"/>

<signal unit="" scale="1" timestamp="2015-09-03T00:07:53.638" name="02_03_18_alarm_word_5" value="0"/>

<signal unit="" scale="1" timestamp="2015-09-03T00:07:54.747" name="02_03_19_int_init_fault" value="0"/>

<signal unit="" scale="1" timestamp="2015-09-03T00:07:56.884" name="02_03_11_follower_mcw" value="0"/>

<signal unit="" scale="1" timestamp="2015-09-03T00:07:58.405" name="02_03_13_aux_status_word_3" value="1030"/>

<signal unit="" scale="1" timestamp="2015-09-03T00:07:59.806" name="02_03_14_aux_status_word_4" value="0"/>

<signal unit="" scale="1" timestamp="2015-09-03T00:08:00.485" name="02_03_16_alarm_word_4" value="0"/>

</signals>

</drive>

</message>

我尝试了多个选项(使用了XML包)但我无法将其转换为数据框/结构化形式。

## method 1
 result <- xmlParse(file = "test.xml")
 print(result)

# method 2
 xmldataframe <- xmlToDataFrame("test.xml")
 print(xmldataframe) 

# method 3
 xmldoc <- xmlParse(file = test.xml)
 rootNode <- xmlRoot(xmldoc)
 xmlSApply(rootNode,function(x) xmlSApply(x, xmlValue))
 cd.catalog <- data.frame(t(data),row.names=NULL)

没有结果/ xmldataframe / cd.catalog有结构化形式的数据框/列表,可用于进一步分析。

1 个答案:

答案 0 :(得分:1)

希望您正在寻找类似的东西:

library(xml2)
library(dplyr)
xml_doc <- read_xml("test.xml")

df <- xml_doc %>% 
  xml_find_all("//signal") %>% 
  xml_attrs() %>%
  unlist() %>%
  matrix(ncol=5, byrow=T) %>%
  as.data.frame(stringsAsFactors=FALSE)
colnames(df) <- c('unit','scale','timestamp','name','value')
df