R XML到数据帧子集
我有一大堆XML格式的聊天记录。我在下面包括了一个示例记录。我不需要整张唱片,只需要三样东西。第一个是@realTimeID属性。第二项/第三项是当source==“PostChat”时varValue中的值。这将包括1-10之间的数值。可能还有第二个值包含文本条目。只有少数记录将包含这些“PostChat”值 我想要的是一个数据框,其中一列表示realTimeID,然后两列表示数值和可能的文本值。如果我可以得到一个数据帧,其中一列表示realTimeID,另一列表示value,我就可以很好地处理其中的数据 以下是一些示例数据:R XML到数据帧子集,r,xml,R,Xml,我有一大堆XML格式的聊天记录。我在下面包括了一个示例记录。我不需要整张唱片,只需要三样东西。第一个是@realTimeID属性。第二项/第三项是当source==“PostChat”时varValue中的值。这将包括1-10之间的数值。可能还有第二个值包含文本条目。只有少数记录将包含这些“PostChat”值 我想要的是一个数据框,其中一列表示realTimeID,然后两列表示数值和可能的文本值。如果我可以得到一个数据帧,其中一列表示realTimeID,另一列表示value,我就可以很好地处
<Report account="12345" end_time="2016-07-01T00:00:59+00:00" limit="10000" more_sessions="true" start_time="2016-06-11T00:00:00+00:00" user="smith">
<Session id="ID1536678170" realTimeID="4768543970">
<Visitor id="1131902386012684">
<ip>123.456.789</ip>
<agent>Chrome 51.0.2704.63</agent>
<host/>
<chatReferer> foo </chatReferer>
<GeoInfo>
<geoCity/>
<geoConType/>
<geoCountry>USA</geoCountry>
<geoIP>123.456.789</geoIP>
<geoISP>USA ISP</geoISP>
<geoOrg>NA</geoOrg>
<geoPost/>
<geoReg/>
</GeoInfo>
</Visitor>
<Chat end_time="2016-06-11T21:46:14+00:00" start_time="2016-06-11T21:25:59+00:00">
<line by="info" time="2016-06-11T21:25:59+00:00">
<Text>Please do not post credit card or other sensitive data in this window. </Text>
</line>
<line by="info" time="2016-06-11T21:26:03+00:00">
<Text>You are now chatting with John.</Text>
</line>
<line by="John" repId="ID2447" time="2016-06-11T21:28:04+00:00">
<HTML><span dir="ltr">Hi sir</span></HTML>
</line>
<line by="John" repId="ID2447" time="2016-06-11T21:28:15+00:00">
<HTML><span dir="ltr">How may i help you ?</span></HTML>
</line>
<line by="you" time="2016-06-11T21:28:16+00:00">
<Text>Hi John. Im Bob. I have a technical question.</Text>
</line>
</Chat>
<VarValues>
<varValue id="ID917165" name="DisconnectedBy" source="Internal" sourceName="null" time="2016-06-11T21:46:14+00:00">RepStoppedChat</varValue>
<varValue id="ID922205" name="language" source="MonitorTag" sourceName="null" time="2016-06-11T21:23:46+00:00">English</varValue>
<varValue id="ID1317606" name="pageLoadTime" source="MonitorTag" sourceName="null" time="2016-06-11T21:23:46+00:00">88 sec</varValue>
<varValue id="ID1323660" name="survey90990357" source="Operator" sourceName="null" time="2016-06-11T21:32:38+00:00">Incomplete (INC) - customer abandons</varValue>
<varValue id="ID1372749" name="LP_Visitor_Category" source="Internal" sourceName="null" time="2016-06-11T21:23:43+00:00">0</varValue>
<varValue id="ID1617100" name="live_engage_control_group" source="Internal" sourceName="null" time="2016-06-11T21:23:43+00:00">false</varValue>
<varValue id="ID3647561" name="rerouteFlag" source="Rule Engine" sourceName="null" time="2016-06-11T21:23:46+00:00">true</varValue>
<varValue id="ID3665417" name="operatorName" source="Rule Engine" sourceName="null" time="2016-06-11T21:26:03+00:00">John Doe</varValue>
<varValue id="ID3730453" name="RenameFlag" source="Rule Engine" sourceName="null" time="2016-06-11T21:23:46+00:00">true</varValue>
<varValue id="ID3742796" name="PT-EligibleInSession" source="Rule Engine" sourceName="null" time="2016-06-11T21:24:43+00:00">Yes</varValue>
<varValue id="ID3834774" name="survey88140234" source="PostChat" sourceName="null" time="2016-06-13T04:44:54+00:00">10</varValue>
<varValue id="ID3834774" name="survey88140234" source="PostChat" sourceName="null" time="2016-06-13T04:44:54+00:00">Great Experience. Thanks for the help!</varValue>
</VarValues>
<Reps>
<Rep endTime="2016-06-11T21:46:14+00:00" id="ID2447" order="1" repName="John Doe" startTime="2016-06-11T21:26:03+00:00">John</Rep>
</Reps>
</Session>
<Session id="ID1536678170" realTimeID="123456789">
<Visitor id="1131902386012684">
<ip>123.456.789</ip>
<agent>Chrome 51.0.2704.63</agent>
<host/>
<chatReferer> foo </chatReferer>
<GeoInfo>
<geoCity/>
<geoConType/>
<geoCountry>USA</geoCountry>
<geoIP>123.456.789</geoIP>
<geoISP>USA ISP</geoISP>
<geoOrg>NA</geoOrg>
<geoPost/>
<geoReg/>
</GeoInfo>
</Visitor>
<Chat end_time="2016-06-11T21:46:14+00:00" start_time="2016-06-11T21:25:59+00:00">
<line by="info" time="2016-06-11T21:25:59+00:00">
<Text>Please do not post credit card or other sensitive data in this window. </Text>
</line>
<line by="info" time="2016-06-11T21:26:03+00:00">
<Text>You are now chatting with John.</Text>
</line>
<line by="John" repId="ID2447" time="2016-06-11T21:28:04+00:00">
<HTML><span dir="ltr">Hi sir</span></HTML>
</line>
<line by="John" repId="ID2447" time="2016-06-11T21:28:15+00:00">
<HTML><span dir="ltr">How may i help you ?</span></HTML>
</line>
<line by="you" time="2016-06-11T21:28:16+00:00">
<Text>Hi John. Im Bob. I have a technical question.</Text>
</line>
</Chat>
<VarValues>
<varValue id="ID917165" name="DisconnectedBy" source="Internal" sourceName="null" time="2016-06-11T21:46:14+00:00">RepStoppedChat</varValue>
<varValue id="ID922205" name="language" source="MonitorTag" sourceName="null" time="2016-06-11T21:23:46+00:00">English</varValue>
<varValue id="ID1317606" name="pageLoadTime" source="MonitorTag" sourceName="null" time="2016-06-11T21:23:46+00:00">88 sec</varValue>
<varValue id="ID1323660" name="survey90990357" source="Operator" sourceName="null" time="2016-06-11T21:32:38+00:00">Incomplete (INC) - customer abandons</varValue>
<varValue id="ID1372749" name="LP_Visitor_Category" source="Internal" sourceName="null" time="2016-06-11T21:23:43+00:00">0</varValue>
<varValue id="ID1617100" name="live_engage_control_group" source="Internal" sourceName="null" time="2016-06-11T21:23:43+00:00">false</varValue>
<varValue id="ID3647561" name="rerouteFlag" source="Rule Engine" sourceName="null" time="2016-06-11T21:23:46+00:00">true</varValue>
<varValue id="ID3665417" name="operatorName" source="Rule Engine" sourceName="null" time="2016-06-11T21:26:03+00:00">John Doe</varValue>
<varValue id="ID3730453" name="RenameFlag" source="Rule Engine" sourceName="null" time="2016-06-11T21:23:46+00:00">true</varValue>
<varValue id="ID3742796" name="PT-EligibleInSession" source="Rule Engine" sourceName="null" time="2016-06-11T21:24:43+00:00">Yes</varValue>
</VarValues>
<Reps>
<Rep endTime="2016-06-11T21:46:14+00:00" id="ID2447" order="1" repName="John Doe" startTime="2016-06-11T21:26:03+00:00">John</Rep>
</Reps>
</Session>
</Report>
123.456.789
铬51.0.2704.63
福
美国
123.456.789
美国ISP
NA
请不要在此窗口中张贴信用卡或其他敏感数据。
你现在正在和约翰聊天。
span dir=“ltr”你好,先生/span
span dir=“ltr”我能为您做些什么?/span
嗨,约翰。我是鲍勃。我有一个技术问题。
重复停止聊天
英语
88秒
不完整(INC)-客户放弃
0
假的
真的
无名氏
真的
对
10
很好的经验。谢谢你的帮助!
约翰
123.456.789
铬51.0.2704.63
福
美国
123.456.789
美国ISP
NA
请不要在此窗口中张贴信用卡或其他敏感数据。
你现在正在和约翰聊天。
span dir=“ltr”你好,先生/span
span dir=“ltr”我能为您做些什么?/span
嗨,约翰。我是鲍勃。我有一个技术问题。
重复停止聊天
英语
88秒
不完整(INC)-客户放弃
0
假的
真的
无名氏
真的
对
约翰
我可以读入数据:
library(XML)
dat <- xmlInternalTreeParse("data/sessions6.xml", useInternalNodes = T)
库(XML)
dat对于更新的xml文件,您可以:
require(xml2)
require(tidyverse)
doc <- read_xml(path_to_file)
sessions <- doc %>% xml_find_all("Session")
realTimeID <- sessions %>% map(xml_attr, "realTimeID")
varValue <- sessions %>%
map(xml_find_all, ".//varValue[@source='PostChat']") %>%
map(xml_text)
map2_df(realTimeID, varValue, ~tibble(realTimeID = .x, value = .y[1], text = .y[2]))
require(xml2)
要求(整洁的人)
doc%
地图(xml_文本)
map2_df(realTimeID,varValue,~tibble(realTimeID=.x,value=.y[1],text=.y[2]))
这给了你:
# A tibble: 2 × 3
realTimeID value text
<chr> <chr> <chr>
1 4768543970 10 Great Experience. Thanks for the help!
2 123456789 <NA> <NA>
#一个tible:2×3
实时ID值文本
14768543970 10伟大的经验。谢谢你的帮助!
2 123456789
您可以使用type\u convert
将更新的xml文件的列类型更改为:
require(xml2)
require(tidyverse)
doc <- read_xml(path_to_file)
sessions <- doc %>% xml_find_all("Session")
realTimeID <- sessions %>% map(xml_attr, "realTimeID")
varValue <- sessions %>%
map(xml_find_all, ".//varValue[@source='PostChat']") %>%
map(xml_text)
map2_df(realTimeID, varValue, ~tibble(realTimeID = .x, value = .y[1], text = .y[2]))
require(xml2)
要求(整洁的人)
doc%
地图(xml_文本)
map2_df(realTimeID,varValue,~tibble(realTimeID=.x,value=.y[1],text=.y[2]))
这给了你:
# A tibble: 2 × 3
realTimeID value text
<chr> <chr> <chr>
1 4768543970 10 Great Experience. Thanks for the help!
2 123456789 <NA> <NA>
#一个tible:2×3
实时ID值文本
14768543970 10伟大的经验。谢谢你的帮助!
2 123456789
您可以使用type\u convert
更改键入的列每个XML是否仅携带一个或两个varValues
,以及@postChat
?对于包含3个变量的一行df?每个“记录”的变量值不应超过2个“PostChat”。但是,大多数记录都没有任何PostChat VAR值。我发布了一条“记录”的XML。我的每个xml文件实际上都有10k个列表。每个xml是否只携带一个或两个带有@postChat
的varValues
?对于包含3个变量的一行df?每个“记录”的变量值不应超过2个“PostChat”。但是,大多数记录都没有任何PostChat VAR值。我发布了一条“记录”的XML。我的每个xml文件实际上都有10k个列表。感谢您的输入。我认为,当某些realTimeID值没有任何PostChat varValue时,这种方法不起作用。我已经更新了上面的XML示例,使其更加完整,并包含了第二个没有任何PostChat值的会话记录。感谢您的输入。我认为,当某些realTimeID值没有任何PostChat varValue时,这种方法不起作用。我已经更新了上面的XML示例,使其更加完整,并包含了第二个没有任何PostChat值的会话记录。